1 Replies - 9388 Views - Last Post: 23 February 2007 - 06:38 AM Rate Topic: -----

#1 pavani2006  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 22-February 07

Find The Longest Word in a Paragraph

Posted 22 February 2007 - 07:57 PM

Would like code in C
#include <iostream>
#include <sstream>
#include <string>
#include <vector>  // for the vector option
 
using namespace std;
 
int main()
{
  string sentence, word;
  // create a string vector to hold the words
  vector<string> sV;
 
  cout << "Enter a sentence: ";
  getline(cin, sentence);
 
  // put the sentence into a stream
  istringstream instr(sentence);
 
  // the >> operator separates the stream at a whitespace
  while (instr >> word)
  {
	cout << word << endl;
	// optionally store each word in a string vector
	// could use an array, but a vector is easier
	sV.push_back(word);
  }
 
  // now let's look at the vector
  cout << "You typed " << sV.size() << " words:\n";
  for(int k = 0; k < sV.size(); k++)
  {
	cout << sV[k] << endl;
  }  
  
  cin.sync();  // purge enter
  cin.get();   // console wait
  return 0;
}


~Admin Edit: Added [code] tags, removed all caps from title, fixed spelling mistakes in title

Is This A Good Question/Topic? 0
  • +

Replies To: Find The Longest Word in a Paragraph

#2 qdoom  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 31-August 06

Re: Find The Longest Word in a Paragraph

Posted 23 February 2007 - 06:38 AM

Could you, please, provide more info?
If the only thing, that you want to achieve is to find the longest word from the entered ones, then you should use functions from <string> library: length() or size(). They both return the size/length/number of elements of the string.
word.size()
sV[0].size()
sV[1].length()

I hope, that's what you wanted to know.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1