Lets a string contains "Hello 1 2 3 4 5"
how do I print just Hello and not everything else.
Is there a method to print until the space.
I know I can print the first 5 characters with a loop, but i want a different method to stop just at the space.
Lets a string contains "Hello 1 2 3 4 5"
how do I print just Hello and not everything else.
Is there a method to print until the space.
I know I can print the first 5 characters with a loop, but i want a different method to stop just at the space.
The function strtok is a possible option; I have not used it in years. So, I will be no further help.
strtok - C++ Reference
Tim S.
If you don't mind hacking up your original string, you can do:
Otherwise, maybe:Code:*strchr(str, ' ') = '\0'; printf("%s", str);
Code:char *s; for(s = str;*s != ' ';++s) putchar(*s);
If you understand what you're doing, you're not learning anything.
I thought the following might help from the string.h library. C string functions
You would have to write a function to "slice" the non-numeric chars out of your string but you could use strpbrk to determine locations of the numeric values.
If you coppied it one char at a time you could write a while loop with an exit condition of NULL and copy the non-numeric chars one at a time into your new array. You'd have to remember to manually add a NULL termination to the end of the new array afterwards.
char *strpbrk( const char *s1, const char *s2)
returns a pointer to the first instance in s1 of any character found in s2. Returns a NULL pointer if no characters from s2 are encountered in s1.