Skip to content

Instantly share code, notes, and snippets.

@phosphore
Created July 1, 2016 15:38
Show Gist options
  • Save phosphore/49d2afd28dc197ed58e1877eccf28f02 to your computer and use it in GitHub Desktop.
Save phosphore/49d2afd28dc197ed58e1877eccf28f02 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdio.h>
int words(const char sentence[ ])
{
int counted = 0; // result
// state:
const char* it = sentence;
int inword = 0;
do switch(*it) {
case '\0':
case ' ': case ',': case '.': case '\n':
if (inword) { inword = 0; counted++; }
break;
default: inword = 1;
} while(*it++);
return counted;
}
int main(int argc, const char *argv[])
{
char sentence[140];
scanf("%139[0-9a-zA-Z ]", sentence);
printf("%d\n", words(sentence));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment