I am trying to solve the following problem in C language. The input is taken as strings and user enters each string in new line. While user enters "END" , the program must stop taking input and should display the number of lines , words and characters. To achieve this I wrote the following code:
#include<stdio.h>
#include<string.h>
void main(){
char a[100], b = {'E', 'N', 'D'};
int i, k, word = 0, chr = 0, line = 0 ;
printf("Enter paragraph (enter END to stop)\n");
gets(a);
k = strcmp(a,b);
while (k != 0 ){
line++;
for (i = 0; a[i] != '\0'; i++){
if (a[i] == ' ')
word++;
else
chr++;
}
gets(a);
k = strcmp(a,b);
}
printf("%d %d %d\n", line, word, chr );
}
In the above code I assumed that the user is using only space button to give spaces between words. The problems was as soon as I give first input I get an error message saying program has stopped working.
I am not able to see whether my logic is wrong or my usage of syntax is wrong.