extract tokens from string in a loop return
program.c copy
?12345678910111213141516 | #include <stdio.h> #include <string.h> int main() { char string[] = "A B C" ; char *stringp = string; char *delim = " " ; char *token; while (stringp != NULL) { token =strsep(&stringp, delim); puts (token); } return 0; } |
Output
?123 | A B C extract tokens from string returnprogram.c copy?
12345678910111213141516171819202122232425262728293031323334353637383940 | #include <stdio.h>#include <string.h> void dump_stringp( char *string, char *stringp) { } int main() { char string[] = "A B C" ; char *stringp = string; const char *delim = " " ; char *token; // stringp is updated to point to the next token 'B' token =strsep(&stringp, delim); printf ( "token ='%s', " , token); if (stringp == NULL) printf ( "stringp == NULL\n" ); else printf ( "stringp - string = %d\n" , stringp - string); // stringp is updated to point to the next token 'C' token =strsep(&stringp, delim); printf ( "token ='%s', " , token); if (stringp == NULL) printf ( "stringp == NULL\n" ); else printf ( "stringp - string = %d\n" , stringp - string); // In case no delimiter was found, stringp is made NULL. token =strsep(&stringp, delim); printf ( "token ='%s', " , token); if (stringp == NULL) printf ( "stringp = NULL\n" ); else printf ( "stringp - string = %d\n" , stringp - string); return 0; } |
Output?123 | token = 'A' , stringp - string = 2 token = 'B' , stringp - string = 4 token = 'C' , stringp = NULL |
|
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理