-
String Parts
Hey,
I have a string like this off a pop server:
+OK 6 42638
I need to get the middle number (6) in an int varible, so i think i just need to split the string up and use atoi(), can someone tell me how to do this baring in mind that the 6 could be any number.
Thanks
TNT
-
Take the array and cycle through it, ignoring whitespace untill a char is found (use a flag ). This means you have reached the 'O' in '"OK". After this, ignore whiespace till the next char is found and start copying, turning copying off when a space is encountered. Cast the copied text with atoi(). The drawbacks of this is that the functionality is limited to that program. A better solution is to write a functin with this prototype (which is reusable):
int get_next_number(char str[], int *next_position);
whereas:
int next = 0;
char string[] = "Get 2 numbers from this 1 string";
int two = get_next_number(string, &next);
int one = get_next_number(string, &next);
"two" should now contain the value '2', and "one" the value '1'.
Hope that helps ;)
-
Something like this will work
Code:
char *pString=NULL;
int iNum=0;
pString=sTextBuffer;
while(*pString!='\0')
{
while(!isdigit(*pString++));//find the first number
pString--;//back up to the first num
iNum=atoi(pString);
break;
}