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

Recommended Answers

All 7 Replies

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";
    }
}

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.

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

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.

any way thankyouuuu very muchhhh :)))

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();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.