Thread: longest word problem

  1. #1
    Registered User
    Join Date
    Dec 2010

    longest word problem

    im having some problems with a menu program for the longest word made of letters only:
    void longest_word(FILE *f,FILE *f1)
              char data[2000];
                int i,len=0,len1=0,k=0;
          if(data[i]==EOF) break;
           {   len++;}
           else if(len>len1)
           {len1=len; k = i-len; len=0;}
    i wrote these words for example:

    the result was:

    is there something wrong or im missing something ?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    You should really work on variable names, comments and indentation to make your code more readable. I'm a bit surprised you got any output since there are numerous problems with your code.
    1. You only allow up to 2000 characters to be read from the file and pay no attention to the newline characters or spaces that may separate words
    2. data is an uninitialized array, and you check the contents against EOF before you ever read anything.
    3. data is an array of chars, but fgetc returns an int (to allow for special values like EOF).
    4. Is len1 the longest length so far? Why not call it something sensible like max_len?
    5. What is k? I think it counts non-letters, but then your code in the bottom for loop makes no sense.
    6. Instead of counting only the letters in each word, you need to break out of the loop if you encounter any non-letters and discard that from your "longest word" search.
    7. You also need another char array to store the current longest word.

    If you know there is going to be one "word" per line in the text file, I would suggest using fgets to read a line at at time, count the characters and discard it if you find anything unsavory. Here's a little pseudo-code to get you going:
    for each line in the file
        read line into buf, remove the '\n' from the end and null terminate it
        initialize word_len = 0
        for i from 0 to the end of buf
            if buf[i] is not a letter
                break out of for loop
            increment word_len
        if (word_len > longest_word_len)
            copy buf into longest_word
    write longest word to file

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    randomstuff is the longest word in your file, with 11 letters compared to 10 for the other things. If you want to say "words that have non-letters stuck to them don't count", then you need to adjust your logic to make sure your words end on a whitespace character, and not just any non-letter.

  4. #4
    Registered User
    Join Date
    Dec 2010
    thx a lot for the fast help
    im going to try it now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in histogram word
    By elwad in forum C Programming
    Replies: 1
    Last Post: 04-18-2009, 09:55 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  4. Word COM problem
    By UnclePunker in forum Windows Programming
    Replies: 6
    Last Post: 01-06-2005, 11:51 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM