Created
February 2, 2017 16:08
-
-
Save kanzash/fe570d266f069b6ef739ab5822ee31ef to your computer and use it in GitHub Desktop.
Allows user to enter a poem, ending with a period. Shows how many words in poem in total, how many lines in poem and how many words per line. No more than 50 characters per line and no punctuation allows.
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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
int main() | |
{ | |
char poem[50][800]; | |
int lines = 0; | |
int words = 0; | |
int i = 0; | |
int j = 0; | |
int k = 0; | |
int ctr = 0; | |
printf("Enter a poem:\n"); | |
while(strncmp(".", (fgets(poem[i], 100, stdin)), 1) != 0){ | |
lines ++; | |
i++; | |
} | |
for(j=0; j<lines; j++) { | |
if(strlen(poem[j]) > 50){ | |
printf("Line of poem is too long!\n"); | |
break; | |
} | |
for(k=0; k<strlen(poem[j]); k++){ | |
if(poem[j][k] == ' ' || poem[j][k] == '\n'){ | |
words++; | |
} | |
} | |
} | |
if(lines == 1 && words > 1){ | |
printf("%d words on %d line\n", words, lines); | |
} | |
else if (words == 1 && lines == 1) { | |
printf("%d word on %d line\n", words, lines); | |
} | |
else { | |
printf("%d words on %d lines\n", words, lines); | |
} | |
for(j=0; j<lines; j++){ | |
for(k=0; k<strlen(poem[j]); k++){ | |
if(isspace(poem[j][k])){ | |
ctr++; | |
} | |
} | |
printf("%d ", ctr); | |
ctr = 0; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment