Skip to content

Instantly share code, notes, and snippets.

@whyrusleeping
Created February 7, 2013 00:42
Show Gist options
  • Save whyrusleeping/4727358 to your computer and use it in GitHub Desktop.
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.
Example input/output.
"the cat in the hat is very fat"
outputs:
the
cat
in
the
hat
is
very
fat
#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