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. Advertisements

  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. Advertisements

  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. Advertisements

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. Mladen Adamovic
    Replies:
    3
    Views:
    15,348
    Mladen Adamovic
    Dec 5, 2003
  2. Replies:
    17
    Views:
    1,050
    Hiran Chaudhuri
    Dec 6, 2005
  3. Richard Bos

    Find a string in another string

    Richard Bos, Aug 11, 2003, in forum: C Programming
    Replies:
    8
    Views:
    2,333
    Glen Herrmannsfeldt
    Aug 16, 2003
  4. Angus
    Replies:
    3
    Views:
    510
  5. petry

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

    petry, Jul 5, 2009, in forum: C Programming
    Replies:
    1
    Views:
    459
    petry
    Jul 5, 2009
  6. Wybo Dekker
    Replies:
    1
    Views:
    614
    Yukihiro Matsumoto
    Nov 15, 2005
  7. vdvorkin
    Replies:
    0
    Views:
    600
    vdvorkin
    Feb 10, 2011
  8. vdvorkin
    Replies:
    3
    Views:
    1,070
    vdvorkin
    Feb 14, 2011
Loading...