0

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?

4
  • 1
    Think carefully what happens in the following: int wlength[1];, wordcount > 1, wlength[wordcount - 1] = symbolcount; Commented Apr 9, 2020 at 7:53
  • 1
    int wlength[1] -- given that you index this array with the word count, 1 is not much. Commented Apr 9, 2020 at 7:53
  • 2
    This: for (i = 0; i <= wordcount;) wlength[0] = longestword; is an infinite loop, because you never update i. (But I like that you are consequent and access wlength[0] instead of wlength[i] in the body. :).) Commented Apr 9, 2020 at 7:56
  • 1
    Your second while loop looks fishy, too. Will shortestword, the length of the shortest word, ever become zero? Also, what is the value of i supposed to be in that loop? How and where do you initialize it? Commented Apr 9, 2020 at 8:01

3 Answers 3

1

The problem is that

int wlength[1];

only declares an array with one element, but you access out of bounds with

shortestword = wlength[i];

This is undefined behavior in C parlance, anything can happen, including what you observe.

To fix this, declare the array with as many elements as you expect i to be. Make sure your loops over i only take values that do not exceed the array element count.

Sign up to request clarification or add additional context in comments.

Comments

1

You have declared an integer array wlength whose size is 2 i.e.,

int wlength[1];

and within the if condition you increment wordcount.

Now assume you have 4 words in a line and wordcount keep on increasing and will be assigned to wlength index but as you have defined the array size 2 , where it overflows. Thus when that is used further in

 shortestword = wlength[i]; 

and

longestword = wlength[i];

it causes junk values to be assigned.

Comments

1

There are several things wrong with your program.

  • You don't allocate space for only one word.
  • You have an infinite loop over i. That's acutally why you don't see any output: The program is stuck in this loop.
  • The second while loop doesn't look as if you knew what you were doing there. I doubt that the condition shortestword < 1 will ever be true. A contunue before other statements makes those statements useless. And what exactly is i here. (Okay, perhaps the while is supposed to be inside the for loop? If so, you need curly braces on the loop body.)

Most of the errors stem from a misunderstanding of the problem. You do not need to store the lengths of all words in order to find the sortest and longest words. Just keeping track of the length of the current word is enough. The algorithm goes like this:

  • set longest to 0.
  • set shortest to a large number.
  • set length to 0.
  • for each character in the input:
    • if it is a white-space character:
      • update longest and shortest if necessary.
      • reset length to 0.
    • otherwise:
      • increase length

This lets you find the longest word in Moby-Dick without having to store more than the current word length. in C, it may look like this:

#include <stdio.h>

int main(void)
{
    int longest = 0;            // length of currently longest word
    int shortest = 0;           // length of currently shortest word

    int length = 0;             // length of current word
    int c = getchar();

    while (c != EOF) {
        if (c == ' ' || c == '\n' || c == '\t') {
            if (length) {
                if (longest == 0 || length < shortest) shortest = length;    
                if (length > longest) longest = length;

                length = 0;
            }
        } else {
            length++;
        }

        c = getchar();
    }

    printf("max: %d\nmin: %d\n", longest, shortest);

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.