473,948 Members | 14,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string find question

With this testprogram:

#include <iostream>
#include <string>

using namespace std;
int main()
{
string::size_ty pe idx;
string s1;
string helloStr("hello ");

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

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<<end l;

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

idx = s2.find(helloSt r);

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<<end l;

}
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 1587
al******@gmail. com wrote:
With this testprogram:

#include <iostream>
#include <string>

using namespace std;
int main()
{
string::size_ty pe idx;
string s1;
string helloStr("hello ");

cout<<"Enter a string ";
cin>s1;
cout << "s1 contains the string: " << s1 << '\n';
idx = s1.find(helloSt r);

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<<end l;

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

idx = s2.find(helloSt r);

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<<end l;

}
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.goo glegroups.com.. .
With this testprogram:

#include <iostream>
#include <string>

using namespace std;
int main()
{
string::size_ty pe 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(helloSt r);

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<<end l;

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

idx = s2.find(helloSt r);

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<<end l;

}
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
1471
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 string For i = 1 To 17 'If IsNull("question" & i & ".Value") Then missedstr = missedstr & " question " & i
51
8347
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 code? many thx!
5
12473
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 inorder not to loose any information?
32
14985
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
17
8189
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 characters") Else Debug.WriteLine("Did NOT find characters!") End If
33
3702
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 has prepared for those who love him" 1 Cor 2:9
6
2224
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 and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
5
4624
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 (-1). I can print out the config string at the beginning of CleanCfgFile(), and the strings are there exactly the way I'm looking for them. So, the question is, what am I doing wrong? Function() {
7
7839
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}" As far as I know yet -- hence this question -- there is no 'one solution fits all', but instead there are several parts that have to be put together to check. What I have so far is, and would like as much feedback as possible to ensure I've...
0
9986
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11174
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10694
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9896
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8256
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7431
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6337
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4540
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3544
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.