find word in a text file

This is a discussion on find word in a text file within the C++ Programming forums, part of the General Programming Boards category; hey guys! i'm a newbie in programming specifically text files. i kinda need some help with this problem that i'm ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    10

    Unhappy find word in a text file

    hey guys!
    i'm a newbie in programming specifically text files.

    i kinda need some help with this problem that i'm trying to solve
    about text files. I'M NOT REALLY ASKING FOR A COMPLETE CODES.

    problem:

    i have a text file, let's call it "vocab.text", the text file contains
    alphabetical words only.
    then i have to create a program that reads the text file and
    finds the longest word that has its REVERSE word.

    example, i found the word "desserts" and its reversal word which is
    "stressed", the 2 words should be the longest words,
    the program should output both "desserts" & "stressed".

    take note that both reversal words are in the text file "vocab.text",
    the programs' job is only to find the longest reversed words like "desserts" & "stressed".



    ANY ADVICE WOULD BE GREAT!
    THANKS IN ADVANCE!


    so far i have this code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
        string str, longest;
        
        ifstream textfile ("vocab.txt");
        
        if (textfile.is_open())
        {
            while (!textfile.eof())
            {
                  getline (textfile,str);
                            
                  reverse (str.begin(), str.end());
                  
                  if (str.size () > longest.size ())
                  longest = str;
                  
             }
             cout << longest <<endl;
             
             textfile.close();
        }
        
        else
        cout << "Unable to open file"<<endl; 
    
        system ("pause");
        return 0;
    }
    Attached Files Attached Files
    Last edited by 26friends26; 02-28-2010 at 08:44 AM.

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    What output are you getting if any? What errors are you having?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    10
    the longest word in reverse.

    actually the function of my program is to reverse all words in the file
    but only output the longest word that has been reversed.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    but what errors are you getting

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    For a given word in a file, after reversing the current word, you must determine whether its reversed word exists.

    Like in your example if "desserts" is the current word , then the existence of "stressed" must be checked in the given file.If the reversed word exists and it is longer than the current maximum length, longest = current_word.

    A brute force solution will take O(n^2) time(worst case), when all the words are distinct.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    10
    Quote Originally Posted by rodrigorules View Post
    but what errors are you getting
    i'm not getting any error, its just that my output is not
    what it should be..

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    10

    Unhappy

    Quote Originally Posted by zalezog View Post
    For a given word in a file, after reversing the current word, you must determine whether its reversed word exists.

    Like in your example if "desserts" is the current word , then the existence of "stressed" must be checked in the given file.If the reversed word exists and it is longer than the current maximum length, longest = current_word.

    A brute force solution will take O(n^2) time(worst case), when all the words are distinct.

    you're right, but the thing is don't have a clear idea/s
    of how to do it...
    how do i find the longest word that has its reverse
    word in the file??.like in my text file vocab.text, my program
    should display "desserts and stressed" but i don't know how.
    any advice?.please.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    10
    how can i close this post??

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, 02:13 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21