Created
July 1, 2016 15:38
-
-
Save phosphore/49d2afd28dc197ed58e1877eccf28f02 to your computer and use it in GitHub Desktop.
This file contains 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 <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