question about string

M

Momo

I have like a string here say: "PROGRAM= sample"
how do i extract this string value so that i can have like
"sample"(notice no space infront of sample) standalone as a char
itself.
any help will be good
 
S

S.Tobias

Momo said:
I have like a string here say: "PROGRAM= sample"
how do i extract this string value so that i can have like
"sample"(notice no space infront of sample) standalone as a char
itself.
any help will be good

You can set a char pointer to the beginning of the string, then
strchr for '=', if successful, increase the pointer, skip spaces
(or all white-space, you can use isspace() for that).
 
T

Thomas Matthews

Momo said:
I have like a string here say: "PROGRAM= sample"
how do i extract this string value so that i can have like
"sample"(notice no space infront of sample) standalone as a char
itself.
any help will be good
Search the web and the newsgroup for:
expression parser
lexer
extracting tokens
ini file
configuration parsing

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
M

Momo

Can you please give some example?? I beening trying to do that, but i
am having no idea how do i start.
Thx for help.
 
A

Al Bowers

Momo said:
> Can you please give some example?? I beening trying to do that, but i
> am having no idea how do i start.
> Thx for help.


See Below.


#include <stdio.h>
#include <ctype.h>
#include <string.h>

char *RemoveLeadingWP(char *s);
char *RemoveTrailingWP(char *s);
char *ParseData(char *s);

int main(void)
{
char *result, test[64] = "PROGRAM= A string without leading "
"and trailing Whitespace ";

result = ParseData(test);
if(result)
printf("Parsed string is: \"%s\"\n",result);
return 0;
}

char *RemoveLeadingWP(char *s)
{
char *s1;

for(s1 = s; *s1 != '\0'; s1++)
if(!isspace((unsigned char)*s1)) break;
if(s1 != s) memmove(s,s1,strlen(s1)+1);
return s;
}

char *RemoveTrailingWP(char *s)
{
char *endp;

for(endp = s+strlen(s); endp!=s; endp-- )
if(!isspace((unsigned char)*(endp-1))) break;
*endp = '\0';
return s;
}

char *ParseData(char *s)
{
char *s1;

if((s1 = strrchr(s,'=')) != NULL)
{
RemoveTrailingWP(++s1);
RemoveLeadingWP(s1);
return s1;
}
return NULL;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,162
Messages
2,570,893
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top