Thread: How to find the longest word in text file?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    20

    How to find the longest word in text file?

    hello my task is ( given a text file. print longest word of each line.) i have tried seweral ways to do it but i failed.
    Code:
    void longest_word(FILE *f,FILE *f1)
       {    
              char data[2000];
                int i,len=0,len1=0,k=0;
         for(i=0;i<2000;i++)
         {   
          if(data[i]==EOF) break;
          data[i]=fgetc(f);
          if((data[i]>='a'&&data[i]<='z')||(data[i]>='A'&&data[i]<='Z'))
           {   len++;}
           else if(len>len1)
           {len1=len; k = i-len; len=0;}
          }
      
        for(i=k;i<k+len1;i++)
         fprintf(f1,"%c",data[i]);
    	     i++;  
       }
    should i use this?


    it would be great that someone help me.
    Last edited by alionas; 03-07-2011 at 12:41 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The problem is that you are working by characters not words...
    Look up the fscanf() function in your C documentation...
    For determining the length of words use strlen() on the text from fscanf...

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    hmm.. i really don't get it wha you say.. could you show me in the program what is wrong? or could you fix the problems becouse my english is very bad

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code seems to be lifted directly from the Internet. Specifically here.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    idk what r u talking about?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    He busted you for plagiarizing. The sample code you originally posted was exactly the same as the code on the link he posted. But then you edited your post to change the code and claimed innocence. By the way, SMS speak is annoying and further hurts your chance of getting help. You should also only go back to edit a posts for small typos and the like, not for changing big swaths of text, since the following posts won't make much sense otherwise.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Meh, copy/paste artist just found a different example right here at C Board.

    If you don't want to actually be a programmer, why did you take a class that requires you to program?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    yes i copy paste i just want to know what should i use the best?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with searching text file
    By zacharyrs in forum C Programming
    Replies: 30
    Last Post: 12-01-2009, 03:13 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM