When I compile this program with gcc:
#include <stdio.h>
/* This program accepts some text as an input and gives the output
* of longest word and shortest word lengths*/
int main(){
int c, i, wordcount, symbolcount, longestword, shortestword;
wordcount = symbolcount = longestword = shortestword = 0;
int wlength[1];
while((c = getchar()) != EOF){
++symbolcount;
if(c == ' ' || c == '\n' || c == '\t'){
++wordcount;
wlength[wordcount];
wlength[wordcount - 1] = symbolcount;
symbolcount = 0;
}
}
for(i = 0;i <= wordcount;)
wlength[0] = longestword;
wlength[i] = shortestword;
while(shortestword < 1){
if(shortestword == longestword){
continue;
++i;
}else if(shortestword < longestword && shortestword > 0){
shortestword = wlength[i];
break;
}
}
for(i = 0; i <= wordcount - 1; ++i){
if(wlength[i] > longestword){
longestword = wlength[i];
}else if(wlength[i] < longestword && wlength[i] > shortestword){
continue;
}else{
wlength[i] = shortestword;
}
}
printf("%d\t%d", longestword, shortestword);
return 0;
}
There are no errors or warnings. But when I try to run it, it accepts the input, but there is no output at all. Even when I press Ctrl + D(I work on a debian based distro), current terminal session is not suspended and the program just keeps running. What can be the problem?
int wlength[1];,wordcount > 1,wlength[wordcount - 1] = symbolcount;int wlength[1]-- given that you index this array with the word count, 1 is not much.for (i = 0; i <= wordcount;) wlength[0] = longestword;is an infinite loop, because you never updatei. (But I like that you are consequent and accesswlength[0]instead ofwlength[i]in the body.:).)whileloop looks fishy, too. Willshortestword, the length of the shortest word, ever become zero? Also, what is the value ofisupposed to be in that loop? How and where do you initialize it?