Thread: some help plz with finding long/shortest words

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    Kaliningrad
    Posts
    5

    some help plz with finding long/shortest words

    Hi to all, I've been trying to write one small program, and I got stuck. I mean, it is finished, it compiles, but does not work as it is supposed to. The task is to keep accepting input from user (words, one by one) until the program spots a four-letter word. That's when it stops and displays the smallest and longest word entered so far.
    What is wrong? Well, the "count" variable does not record the new value when entering the while loop again at all. It keeps being incremented by next entered word... What's more, the length of smallest/largest word is not counted at all.
    Code:
    #include <stdio.h>
    #include <string.h>
     
    #define LEN 20
     
    int main()
    {
     
        int count;
        int i = 0;
        int count_smallest;
        int count_largest;
        char word[LEN];
        char smallest_word[LEN];
        char largest_word[LEN];
        char ch;
     
     
      do { 
           printf("Enter a word: ");
     
           while( (ch = getchar()) != '\n')
                if(i < LEN)
                  word[i++] = ch;
     
           word[i]  = '\0';
     
           count = strlen(word);
          
           count_smallest = strlen(smallest_word);
           count_largest = strlen(largest_word);
     
           if(count <= count_smallest)
             strcpy(smallest_word, word);
           else  
             strcpy(largest_word, word);
           }
        while(count != 4);
     
     
        printf("Smallest word is %d letters long and it is: ",   
                 count_smallest);
        puts(smallest_word);
        printf("\n");
        printf("Largest word is %d letters long and it is: ", 
                 count_largest);
        puts(largest_word);
        printf("\n");
     
    return 0;
    }
    I will appreciate your fresh look at my code...

  2. #2
    UCF Mystic_Skies's Avatar
    Join Date
    Oct 2004
    Posts
    33
    The reason your count variable does not record the new value of the word length is that it keeps being initialized to how long "word" is. If you keep assigning more and more letters to "word" it will just keep getting longer and longer so it will never quit the loop. You'll see what I mean if you enter a four letter word on the first try. Just use some printf statements to see what your variables are doing in the loop. That's the easiest way to see it.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    Kaliningrad
    Posts
    5
    That's exactly what I did in my file on my pc, I put extra printf's, and I noticed that none of the counter which are supposed to find the length of words eith do not record it at all (count_smallest, count_largest), or the count for the main input array just gets bigger...
    I already tried to set count of word array to zero every time it starts the while loop but it does not get help,. unfortunately.

    Basically, the code agian, only with the minor change of count being set to zero when entering the loop...

    the output is:
    Enter a word: hget
    Smallest word is 0 letters long and it is:

    Largest word is 0 letters long and it is: hget

    % !!
    a.out
    Enter a word: hgtaf
    Enter a word: hytesk
    Enter a word: a
    Enter a word: aaaa
    Enter a word: ^C
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
     
    #define LEN 20
     
    int main()
    {
     
        int count;
        int i = 0;
        int count_smallest;
        int count_largest;
        char word[LEN];
        char smallest_word[LEN];
        char largest_word[LEN];
        char ch;
     
     
      do { 
           count = 0;
           printf("Enter a word: ");
     
           while( (ch = getchar()) != '\n')
                if(i < LEN)
                  word[i++] = ch;
     
           word[i]  = '\0';
     
           count = strlen(word);
          
           count_smallest = strlen(smallest_word);
           count_largest = strlen(largest_word);
     
           if(count <= count_smallest)
             strcpy(smallest_word, word);
           else  
             strcpy(largest_word, word);
           }
        while(count != 4);
     
     
        printf("Smallest word is %d letters long and it is: ", count_smallest);
        puts(smallest_word);
        printf("\n");
        printf("Largest word is %d letters long and it is: ", count_largest);
        puts(largest_word);
        printf("\n");
     
    return 0;
    }

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
           while( (ch = getchar()) != '\n')
                if(i < LEN)
                  word[i++] = ch;
    What are you trying to do here, because I don't think it is doing what you want it to.
    Code:
           count_smallest = strlen(smallest_word);
           count_largest = strlen(largest_word);
    What is the string length on the first iteration of the loop?

  5. #5
    Registered User
    Join Date
    Feb 2005
    Location
    Kaliningrad
    Posts
    5
    Thanks, but I rewrote the whole thing again, from scratch, used a different compiler, and now it works perfect. The problem must been in the order of statements, because I basically wrote the same thing, but in slightly different order, as I noticed. It helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  2. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. using strlen and finding shortest and longest words
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-30-2001, 06:09 PM