473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

string find question

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 '06 #1
2 1563
al******@gmail.com 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.
Oct 20 '06 #2
<al******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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?

Oct 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sparks | last post by:
I know this is a stupid question but I can't find the answer. Please tell me where to find reference to this so I can print it out and staple it to my head.. dim I as integer dim missedstr as...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
5
by: Dave | last post by:
I'm receiving info from a com port into a string. I gradually process the string which constantly shortens it. The question is how long can a string be before I need to write some info to disk...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
17
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
5
by: PaulH | last post by:
I have a function that is stripping off some XML from a configuration file. But, when I do a search for the pieces I want to strip, the std::string::find() function always returns std::string::npos...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.