Thread: Longest word

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    58

    Longest word

    Hello. I am writing program which should find the longest word in .dat file. Here is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
        int i=0,a=0;
        FILE *in_file;
        char ch;
        char word[20],longest_word[20],str[200];
    
        if((in_file = fopen("word.dat", "r"))==NULL)
        {
        printf("File %s cannot be openned\n", "words.dat");
        system("pause");
        exit(8);
        }
        else
        {
        while (!feof(in_file)) 
        {
              fgets(str, 199 , in_file);
              while (str[a]!= '\n')
              {
                    ch = str[a];
                    if (((int)ch>=97 && (int)ch<=122)||((int)ch>=65 && (int)ch<=90))
                    {
                    word[i]=ch;
                    i++;
                    }
                    else
                    {
                    if ((strlen(word))>(strlen(longest_word)))
                    {
                    strcpy ( word, longest_word );
                    i=0;
                    }
                    }
              }
              a=0;
        }
              
              
              } 
              printf ("%s \n",longest_word);
              system("pause");
              fclose(in_file);
              
    }
    I don't know what is wrong with the code i think it should work but i am not getting any output. Maybe someone could help me?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You definitely have the general idea right, but there are a lot of things wrong with your code:
    • Your indentation is horrible. If it's hard to read the code, it's hard to find mistakes, but easy to make them. Pick one of the first 3 styles listed here: Indent style - Wikipedia, the free encyclopedia.
    • You correctly declared main to return an int, but you never return an int. Put a return 0; at the end of main. Also, you should use return 8; instead of calling exit().
    • Try to avoid stuff like system("pause"), it makes it hard for us non-Windows people to help you. Besides, there are better solutions: FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com.
    • Don't use feof to control your while loop. Read this: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com. Then, fix your loop control to use the return value of fgets.
    • fgets ensures that it will put a '\0' in there, so there's no need to use 199 instead of 200. But better yet, make it so if you change the size of str, you don't need to change the fgets call: fgets(str, sizeof(str), in_file).
    • Make word, longest_word and str the same size. If your .dat file is one word per line (as your code suggests), and the biggest word is 20 chars, then str only needs to be 22 chars (to hold the new line and null char), and word and longest_word should be 21 chars (to hold the null terminator). This will help guard against buffer overflows a little bit.
    • Don't use magic numbers for checking if a character is a letter. Those numbers are cryptic, and they're specific to ASCII, which isn't used everywhere. #include <ctype.h> and use the isalpha() function. It makes sense and it's portable.
    • You never null terminate word, so the strlen might fail.
    • Your strcpy is backwards. You're overwriting word with the contents of longest_word. It should be strcpy(longest_word, word).
    • You need to initialize longest_word to an empty string (set it's first char to '\0'), otherwise it has garbage, and strlen(longest_word) could be huge, meaning you never find a longer word in your .dat file.
    • If your .dat file is one word per line, there's an easier way than what you're doing. After fgets, trim the new line from str and just use strlen(str). See below for an example.
    That's all I got in my quick review. Should be enough to keep you going for a while.

    Code:
    char *p;
    p = strchr(str, '\n');
    if (p)
        *p = '\0';  // If str has a newline, "trim" it, i.e. replace it with a '\0'
    ...
    if (strlen(str) > strlen(longest_word))
        strcpy(longest_word, str)
    EDIT: Also, you don't need the extra variable ch, you can just use str[a] in it's place. You should also define a constant for the file name, so you don't have to make multiple changes if you change the file name. #define FILENAME "words.dat" at the top, and replace every instance of "words.dat" in your program (lines 11 & 13) with FILE_NAME
    Last edited by anduril462; 03-09-2012 at 01:27 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    My .dat file not one word per line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. How to find the longest word in text file?
    By alionas in forum C Programming
    Replies: 7
    Last Post: 03-07-2011, 01:05 PM
  3. longest word problem
    By zinzo in forum C Programming
    Replies: 3
    Last Post: 12-19-2010, 11:34 AM
  4. Please help this algorithm. "Longest word"
    By mike2008 in forum Tech Board
    Replies: 4
    Last Post: 10-02-2010, 09:48 AM
  5. Finding the longest word in a string.
    By whoami2 in forum C Programming
    Replies: 2
    Last Post: 04-14-2007, 11:00 AM