Skip to content

Instantly share code, notes, and snippets.

@kanzash
Created February 2, 2017 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanzash/fe570d266f069b6ef739ab5822ee31ef to your computer and use it in GitHub Desktop.
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.
#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