Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,832 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,538 people online right now. Registration is fast and FREE... Join Now!




Find The Longest Word in a Paragraph

 
Reply to this topicStart new topic

Find The Longest Word in a Paragraph

pavani2006
22 Feb, 2007 - 06:57 PM
Post #1

New D.I.C Head
*

Joined: 22 Feb, 2007
Posts: 15



Thanked: 2 times
My Contributions
Would like code in C
CODE

#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
User is offlineProfile CardPM
+Quote Post

qdoom
RE: Find The Longest Word In A Paragraph
23 Feb, 2007 - 05:38 AM
Post #2

D.I.C Head
**

Joined: 31 Aug, 2006
Posts: 82


My Contributions
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.
CODE
word.size()
sV[0].size()
sV[1].length()

I hope, that's what you wanted to know.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:35AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month