How can I divide the input string of readline()? For example in cosole i typed "This is me".
How can I divide this to
readline() string 1 = This
readline() string 2 = is
readline() string 3 = me
thanks
Printable View
How can I divide the input string of readline()? For example in cosole i typed "This is me".
How can I divide this to
readline() string 1 = This
readline() string 2 = is
readline() string 3 = me
thanks
Consider strtok.
readline is not a standard function, so I don't know for certain what type of data it returns, but I assume it's a char array/string. If so, use sscanf with the "%s" modifier, which skips over white space, or use strtok (my preference). Google should have plenty of examples of both.
lets say I used strtok to divide this line of strings. how can i assign each string to a variable? like string1 = This, string2 = is, string3=me
Code:char *pch;
char string[50];
char first[10], second[10], third[10];
strcpy( string, "this is me" );
printf( "%s\n", string );
pch = strtok( string, " " );
strcpy( first, pch );
pch = strtok( NULL, " " );
strcpy( second, pch );
pch = strtok( NULL, " " );
strcpy( third, pch );
printf( "%s\n%s\n%s\n", first, second, third );
// ect