473,385 Members | 1,396 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,385 software developers and data experts.

string.fine(string+string...

Hi

after reading the docs, I was hoping this would work, but I can't find
out why.

string a = "I ";
string b = "am";
int ego;
if(sentence.find((a+b).c_str(),0)!= string::npos) ego++;

from the docs
****************
size_type find( const string& str, size_type index );
size_type find( const char* str, size_type index );
size_type find( const char* str, size_type index, size_type length );
size_type find( char ch, size_type index );

thanks
Aug 18 '06 #1
5 1436
Gary Wessle <ph****@yahoo.comwrites:
Hi

after reading the docs, I was hoping this would work, but I can't find
out why.

string a = "I ";
string b = "am";
int ego;
if(sentence.find((a+b).c_str(),0)!= string::npos) ego++;

from the docs
****************
size_type find( const string& str, size_type index );
size_type find( const char* str, size_type index );
size_type find( const char* str, size_type index, size_type length );
size_type find( char ch, size_type index );

thanks
the fix
if(sentence.find(a + b,0)!= string::npos) ego++;

I don't know why this worked, I tried "before posting"

if(sentence.find(a+b,0)!= string::npos) ego++; for no avail.

it seams then that a + b is not the same as a+b in this case.
Aug 18 '06 #2

It works for me. Can you post the complete code (including what
"sentence" is)? And ego's
scope is not clear, you might need initializing it if it's in local
scope.

BTW, you should be able to call find using;

sentence.find(a+b)

you can skip the 0 index and converstion to C-string.

Tolga Ceylan

Aug 18 '06 #3
Gary Wessle schrieb:
>after reading the docs, I was hoping this would work, but I can't find
out why.

string a = "I ";
string b = "am";
int ego;
if(sentence.find((a+b).c_str(),0)!= string::npos) ego++;

from the docs
****************
size_type find( const string& str, size_type index );
size_type find( const char* str, size_type index );
size_type find( const char* str, size_type index, size_type length );
size_type find( char ch, size_type index );

thanks

the fix
if(sentence.find(a + b,0)!= string::npos) ego++;

I don't know why this worked, I tried "before posting"

if(sentence.find(a+b,0)!= string::npos) ego++; for no avail.

it seams then that a + b is not the same as a+b in this case.
It is the same. Write complete and compileable code and post the error
message from your compiler.

--
Thomas
Aug 18 '06 #4
"Gary Wessle" <ph****@yahoo.comwrote in message
news:87************@yahoo.com...
Hi

after reading the docs, I was hoping this would work, but I can't find
out why.
....you can't find out why you were hoping it would work?
....you can't find out why it did work?
What do you mean?
string a = "I ";
string b = "am";
int ego;
if(sentence.find((a+b).c_str(),0)!= string::npos) ego++;
The call to c_str() there will cause you headaches. (a+b) creates a
temporary string, which will be destroyed when it is no longer needed. This
can be after the call to c_str() but before the call to find(); in which
case, the pointer returned by c_str() will point to memory which has since
been deallocated. This is a bad thing.

One way to solve this problem is to make an explicit temporary:

string tmp = a + b;
if(sentence.find(tmp.c_str(),0)!=string::npos) ego++;

this works because tmp lasts until the end of the current scope.

A better solution, which others have posted elsethread, is to leave out the
call to c_str() entirely.

Philip

Aug 18 '06 #5
Philip Potter wrote:
"Gary Wessle" <ph****@yahoo.comwrote:
>>
if(sentence.find((a+b).c_str(),0)!= string::npos) ego++;

The call to c_str() there will cause you headaches. (a+b) creates a
temporary string, which will be destroyed when it is no longer needed. This
can be after the call to c_str() but before the call to find();
Actually temporary objects last until the end of the full-expression
in which they were created -- in this case, until the semi-colon.

The code should work, although it is unnecessarily bloated:
sentence.find( a + b )
is sufficient.

Aug 19 '06 #6

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

Similar topics

4
by: Ken Fine | last post by:
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string, return the highlighted search term along with the...
1
by: Dean Bortell | last post by:
I have a program that encrypts a datetime objects "to long datetime string" string to and encrypted sequence. The problem is this: if I use the following source code it works (key is declared...
14
by: msnews.microsoft.com | last post by:
How can I encrypt and decrypt string?
5
by: gmccallum | last post by:
I am trying to convert the value of a string to a defined enum value such as follows. public enum MyEnum { One, Two }; string MyString = "One"; // or even this is fine string MyString2 =...
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...
24
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET...
8
by: Csaba Gabor | last post by:
I wrote a .repeat(n) function for strings which seemed to work fine: String.prototype.repeat = function(n) { // repeats the string n times if (n<1) return ""; if (n<2) return this; for (var...
1
by: peterbe | last post by:
This has always worked fine for me. Peter fine Now if I do it with a unicode string: Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.4/string.py", line...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.