0

heyy all ,, i'm trying to get the middle word of sentence contains 3 words using find and substr..
this is the code :

#include <iostream>
#include <string>
using namespace std;

void main()
{
    string sentence;
    cout << "enter a three-words sentence :";
    getline(cin, sentence);

string middle = sentence.substr(sentence.find(" ") + 1, sentence.find(" ")); // find the middle word 
    cout << middle << endl; //Print the middle word

    system("pause");
}

so guys what is the solution!!please be helpful :) <3

Edited by saja.omarii.7

4
Contributors
7
Replies
30
Views
2 Years
Discussion Span
Last Post by saja.omarii.7
Featured Replies
  • 1

    Reading the documentation for `substr` and `find` would be helpful, it highlights that the second argument is a count of characters, not a position in the string. `find` gives you the position in the string and also accepts a starting position, so a little munging of data is needed: #include … Read More

  • 1

    > actually till now we dont use string::size_type :( Till now, that's what we call learning. ;) `size_type` is the type `string::find` returns, and it's the type `string::substr` accepts. Read More

1

Reading the documentation for substr and find would be helpful, it highlights that the second argument is a count of characters, not a position in the string. find gives you the position in the string and also accepts a starting position, so a little munging of data is needed:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string sentence;

    cout << "enter a three-words sentence :";

    if (getline(cin, sentence))
    {
        string::size_type middle_begin = sentence.find(" ") + 1;
        string::size_type middle_end = sentence.find(" ", middle_begin);

        string middle = sentence.substr(middle_begin, middle_end - middle_begin);

        cout << "'" << middle << "'\n";
    }
}
1

Nevermind. Deceptikon you beat me to it. I will say that you can make this work for sentences with more then 3 words using stringstreams and a vector.

Edited by NathanOliver

1

actually till now we dont use string::size_type :(

Till now, that's what we call learning. ;) size_type is the type string::find returns, and it's the type string::substr accepts.

0

heyy all ,, i'm trying to get the middle word of sentence contains 3 words using find and substr ...

Do you really have to use 'find' and 'substr' ?

If not, as 'NathanOliver' has stated, using stringstream objects ... makes for a really nice C++ idiom here ...
(to extract the 2nd word, or all the words, in a line, [or file], of words ... )

I

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;


int main() // Note: C++ uses int main() and returns an int value //
{
    cout << "Enter a sentence :";
    string line;
    getline( cin, line );

    istringstream iss( line ); // construct iss object from line

    vector < string > myStrs; // construct empty vector to hold words

    string word;

    // ok ... can now extract all the 'words' from the line

    while( iss >> word ) myStrs.push_back( word ); // fill vector

    cout << "\nYou have " << myStrs.size() << " words in the line.\n";

    // can use [] operator ( or iterators)
    // to access the words in the vector

    // ... rest of code goes here ...

    cout << "\nPress 'Enter' to continue/exit ... ";
    cin.get();
}

Edited by David W: fixed typo

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.