string find question

Discussion in 'C++' started by alacrite@gmail.com, Oct 20, 2006.

  1. Guest

    With this testprogram:

    #include <iostream>
    #include <string>

    using namespace std;


    int main()
    {
    string::size_type idx;
    string s1;
    string helloStr("hello");

    cout<<"Enter a string ";
    cin>> s1;
    idx = s1.find(helloStr);

    if(idx == string::npos)
    cout<<"The string "<<helloStr<<" was not found in s1"<<endl;
    else
    cout<<"The first occurance of 'hello' in s1 is at: "<<idx<<endl;

    string s2("This is a test hello world!");

    idx = s2.find(helloStr);

    if(idx == string::npos)
    cout<<"The string "<<helloStr<<" was not found in s2"<<endl;
    else
    cout<<"The first occurance of 'hello' in s2 is at: "<<idx<<endl;

    }
    Program output:

    Enter a string This is a test hello world!
    The string hello was not found in s1
    The first occurance of 'hello' in s2 is at: 15

    Question why does it not find hello fo s1?
     
    , Oct 20, 2006
    #1
    1. Advertising

  2. Daniel T. Guest

    wrote:

    > With this testprogram:
    >
    > #include <iostream>
    > #include <string>
    >
    > using namespace std;
    >
    >
    > int main()
    > {
    > string::size_type idx;
    > string s1;
    > string helloStr("hello");
    >
    > cout<<"Enter a string ";
    > cin>> s1;


    cout << "s1 contains the string: " << s1 << '\n';

    > idx = s1.find(helloStr);
    >
    > if(idx == string::npos)
    > cout<<"The string "<<helloStr<<" was not found in s1"<<endl;
    > else
    > cout<<"The first occurance of 'hello' in s1 is at: "<<idx<<endl;
    >
    > string s2("This is a test hello world!");
    >
    > idx = s2.find(helloStr);
    >
    > if(idx == string::npos)
    > cout<<"The string "<<helloStr<<" was not found in s2"<<endl;
    > else
    > cout<<"The first occurance of 'hello' in s2 is at: "<<idx<<endl;
    >
    > }
    > Program output:
    >
    > Enter a string This is a test hello world!
    > The string hello was not found in s1
    > The first occurance of 'hello' in s2 is at: 15
    >
    > Question why does it not find hello fo s1?


    Add the line I put above and try running your program again.

    Alternatively, replace the line "cin >> s1;" with "getline( cin, s1 );"

    --
    There are two things that simply cannot be doubted, logic and perception.
    Doubt those, and you no longer have anyone to discuss your doubts with,
    nor any ability to discuss them.
     
    Daniel T., Oct 20, 2006
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    > With this testprogram:
    >
    > #include <iostream>
    > #include <string>
    >
    > using namespace std;
    >
    >
    > int main()
    > {
    > string::size_type idx;
    > string s1;
    > string helloStr("hello");
    >
    > cout<<"Enter a string ";
    > cin>> s1;


    This will only input characters to s1 until the first white space (tab or
    space or enter).

    use
    std::getline( std::cin, s1 );
    to get the whole text upto the carriage return.

    > idx = s1.find(helloStr);
    >
    > if(idx == string::npos)
    > cout<<"The string "<<helloStr<<" was not found in s1"<<endl;
    > else
    > cout<<"The first occurance of 'hello' in s1 is at: "<<idx<<endl;
    >
    > string s2("This is a test hello world!");
    >
    > idx = s2.find(helloStr);
    >
    > if(idx == string::npos)
    > cout<<"The string "<<helloStr<<" was not found in s2"<<endl;
    > else
    > cout<<"The first occurance of 'hello' in s2 is at: "<<idx<<endl;
    >
    > }
    > Program output:
    >
    > Enter a string This is a test hello world!
    > The string hello was not found in s1
    > The first occurance of 'hello' in s2 is at: 15
    >
    > Question why does it not find hello fo s1?
    >
     
    Jim Langston, Oct 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angus
    Replies:
    3
    Views:
    359
  2. petry

    std::string::find vs std::find

    petry, Jul 5, 2009, in forum: C Programming
    Replies:
    1
    Views:
    364
    petry
    Jul 5, 2009
  3. Wybo Dekker
    Replies:
    1
    Views:
    399
    Yukihiro Matsumoto
    Nov 15, 2005
  4. vdvorkin
    Replies:
    0
    Views:
    440
    vdvorkin
    Feb 10, 2011
  5. vdvorkin
    Replies:
    3
    Views:
    871
    vdvorkin
    Feb 14, 2011
Loading...

Share This Page