Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main(void)
- {
- int c, countSpaces = 0;
- printf("Type sentence:n");
- do
- {
- c = getchar();
- if (c == ' ')
- countSpaces = countSpaces + 1;
- }
- while (c != 'n');
- printf("Sentence contains %d Spaces.n", countSpaces);
- return 0;
- }
- if (c != EOF)
- countSpaces = countSpaces + 1;
- }
- while (c != 'n');
- printf("Sentence contains %d Spaces.n", countSpaces - 1);
- #include <stdio.h>
- int main(void)
- {
- int c;
- int countSpaces = 0;
- int countChars = 0;
- puts("Type sentence:");
- do {
- c = getchar();
- countChars += 1;
- if (c == ' ') {
- countSpaces += 1;
- }
- } while (c != 'n');
- printf("Sentence contains %d spaces and %d characters.n", countSpaces, countChars);
- return 0;
- }
- if( condition )
- do something
- whoops this is not in the condition but it sure looks like it is!
- $ ./test
- Type sentence:
- foo bar baz
- Sentence contains 2 spaces and 12 characters.
- // Note, the parenthesis around `c = getchar()` are important.
- while( (c = getchar()) != 'n' ) {
- countChars++;
- if (c == ' ') {
- countSpaces++;
- }
- }
- $ ./test
- Type sentence:
- foo bar baz
- Sentence contains 2 spaces and 11 characters.
- #include <stdio.h>
- int main()
- {
- char *str = calloc(sizeof(char),50);
- int i = 0, count = 0;
- printf("Type sentence:n");
- scanf("%s",str);
- while (str[i++] != '