Created
February 7, 2013 00:42
-
-
Save whyrusleeping/4727358 to your computer and use it in GitHub Desktop.
The challenge problem for today will be to take a very long string and break it into tokens by a given delimiter, and print out each token.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example input/output. | |
"the cat in the hat is very fat" | |
outputs: | |
the | |
cat | |
in | |
the | |
hat | |
is | |
very | |
fat | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void splitString(char *input, char delim) | |
{ | |
int i= 0, j = 0; | |
for(i = 0; input[i] != '\0'; i++) | |
{ | |
if(input[i] == delim) | |
{ | |
while(j < i) //Print out the word | |
putchar(input[j++]); | |
putchar('\n'); | |
j++; //move j past the deliminator | |
} | |
} | |
//case for last word, not followed by delim | |
while(j < i) | |
putchar(input[j++]); | |
putchar('\n'); | |
} | |
int main() | |
{ | |
char *myString = "Hello everyone this is a test of the emergency alert system"; | |
splitString(myString, ' '); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment