A
alacrite
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?
#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?