7 Replies - 11476 Views - Last Post: 22 February 2010 - 03:39 PM Rate Topic: -----

#1 XinJiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-February 10

Finding the longest word program

Posted 20 February 2010 - 10:33 PM

Helo,

I need some help with writing a program that would read a .txt file that consists of some words and then displays the longest word on the screen.

Here is the assignment description:

Write a program that reads this file and finds the longest word that contains only a single vowel (a, e, i, o, u). Output this word (there will actually be several ties for the longest word. Your program only needs to output one of these words).

I am working on the the part of the assignment where I sort out the words and display the longest word in the list. I am having a little trouble with it, how would I get the program to count the length of the words and display one of the longest words?

I noticed that someone had a question about the exact same problem but no one seemed to answer their question.

Here is what I have done so far though:

#include <cstdlib>
#include <iostream>
#include <fstream>


void input (char*);
void play(char*, char[5000], int&, int&);
void wordCount (char*);
void longWord (char*, int);


using namespace std;

int main(){



    ifstream inStream;


    inStream.open("./words.txt");

  char temp[5000];

//  inStream >> len;


  int counter = 0;

  int max_word = -1;

  int posi=0;
 

  int length = (strlen(temp));

  temp[length]= ' ';

 

//  cout<<length<<endl;

 

  for(int i=0; i<=length; i++)

  {

      if(temp[i] !=' ')

      {

          counter++;

      } 

      else if(temp[i]==' ')

      {

          if(counter > max_word)

          {

              max_word = counter;

              posi=i;

          }

          counter = 0;

 

      }

 

 

  }  

  cout <<"Longest word:";

   for(int k=posi-max_word; k<posi; k++)

  {cout<<temp[k];}

 

getchar();

}


    inStream.close( );





    system("PAUSE");
    return EXIT_SUCCESS;
}


This post has been edited by XinJiki: 21 February 2010 - 12:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Finding the longest word program

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Finding the longest word program

Posted 20 February 2010 - 11:03 PM

It should be a pretty straight-forward program with no sorting needed. Just read the file one word at a time. If the word just read contains more than one vowel then read the next word. Otherwise the word contains only one vowel, check it against the previously saved one-vowel word, if there was one. Then go back and read the next word.

Since you are writing a c++ program there is no need to use character arrays or any of the string functions in string.h ( or cstring ). Just read the words directly into std::string object
std::string word;
std::string longest_word;
while( inStream >> word)
{
   // blabla
}
cout << longest_word << '\n';


This post has been edited by Ancient Dragon: 20 February 2010 - 11:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Finding the longest word program

Posted 20 February 2010 - 11:50 PM

The default setting for the iostream >> extract operator is to skip whitespace. So, if you simply output the file to a string variable, the variable will contain each word of the input file in succession.

For example:

...
ifstream ifs ("ReadMe.txt", ifstream::in);

while (ifs) {
	ifs >> word;

        check for vowel count and length...
...



HTH
Was This Post Helpful? 0
  • +
  • -

#4 XinJiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-February 10

Re: Finding the longest word program

Posted 21 February 2010 - 12:10 PM

I tried doing it that way, now I am running into a compilation error.
The compiler keep telling me to declare "size" when I tried declaring it tells me that it is not a type. I know I'm doing something wrong I'm just not sure what it is.

The code that I'm working with now is:
#include <iostream>
#include <fstream>
#include <cstdlib>


std::string word; 
std::string longest_word; 
using namespace std;

struct Sort {
       int size;
};

int main()
{
 ifstream inStream;
 inStream.open("words.txt");
 string  word;
 Sort size;
//int size;

for (int i = 0; i< 1000; i++)

while( inStream >> word) 
{
       
    if (word [i].size () > longest_word.size ())
    longest_word = word[i];
    
    
   
inStream.close( );
} 


cout << longest_word << "\n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Thank you very much!
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Finding the longest word program

Posted 21 February 2010 - 04:30 PM

First, you are missing the declaration for longest_word:

...
string  word;
string longest_word;

Sort size;
...



Second, you don't need, or want, to index into "word" to get it's length:

...
while( inStream >> word) {    
    if (word.size() > longest_word.size())
    longest_word = word;
...



Finally, this is C++. :) Use "endl" instead of "\n":

// cout << longest_word << "\n";
cout << longest_word << endl;



Same amount of typing! :)
Was This Post Helpful? 0
  • +
  • -

#6 XinJiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-February 10

Re: Finding the longest word program

Posted 21 February 2010 - 05:46 PM

Thank you, your help cleared up my compilation error problem. I need to find the longest word out of an list that contains almost 50,000 words. I thought I might need to use the
for (int i = 0; i< 50000; i++)


It reads in the very first word on the list and displays it as the longest word, when it is not.

So far I have this:
#include <iostream>
#include <fstream>
#include <cstdlib>


std::string word; 
std::string longest_word; 
using namespace std;

struct Sort {
       int size;
};

int main()
{
 ifstream inStream;
 
 string  word;
 string longest_word;
 Sort size;
//int size;

inStream.open("words.txt", ifstream ::in);

//for (int i = 0; i< 50000; i++)

while(inStream) {
       
       inStream >> word;
       
    if (word.size () > longest_word.size ())
    longest_word = word;
    
    
   
inStream.close( );
} 


cout << longest_word << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}




I am I doing something that is obvisously wrong?

This post has been edited by XinJiki: 21 February 2010 - 06:01 PM

Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Finding the longest word program

Posted 22 February 2010 - 01:53 PM

That's because you are closing the input file on the first pass of the while() loop: :)

while(inStream) {

	inStream >> word;

	if (word.size () > longest_word.size ())
		longest_word = word;



	inStream.close( );  <<<<<<<<<<<<<<<<<<<<<<<
} 


Was This Post Helpful? 0
  • +
  • -

#8 XinJiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-February 10

Re: Finding the longest word program

Posted 22 February 2010 - 03:39 PM

I did not notice I had that within my loop. Thank you so much, it is finding the longest word now! Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1