Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 14th, 2006, 9:31 PM   #1
gloriasj8
Newbie
 
Join Date: Nov 2006
Posts: 1
Rep Power: 0 gloriasj8 is on a distinguished road
Question Help with comparing strings

Here is my code. I need to show that one string is found in another ex: sip is contained in mississippi, or pop is not contained in mississippi. both string must be cin by the keyboard.


#include <iostream>
#include <string>


using namespace std;

string compare( string str1, string str2);

int main(void)
{


string str1, str2;


cout<<"Enter the first string: ";
cin>>str1;

cout<<"Enter the string to search for: ";
cin>>str2;
string a = compare ( str1, str2);

cout<<"\n"<<str2<<" is contained in "<<str1
;
string b = compare ( str1, str2);
cout<<"\n"<<str1<<" is not contained in "<<str2;

return 0;

}

string compare( string str1, string str2)
{

if (str1 == str2 )
{
return 0;
}


else
{
str2.compare( str1 );
}


}
gloriasj8 is offline   Reply With Quote
Old Nov 14th, 2006, 9:36 PM   #2
Indigno
Professional Programmer
 
Indigno's Avatar
 
Join Date: Dec 2005
Location: Anywhere non-productive
Posts: 267
Rep Power: 0 Indigno is an unknown quantity at this point
Send a message via AIM to Indigno Send a message via MSN to Indigno Send a message via Yahoo to Indigno
string a = compare ( str1, str2);

cout<<"\n"<<str2<<" is contained in "<<str1
;
string b = compare ( str1, str2);
cout<<"\n"<<str1<<" is not contained in "<<str2;
That should be at the end of the line before it.
string compare( string str1, string str2)
{

if (str1 == str2 )
{
return 0;
}


else
{
str2.compare( str1 );
}


}
you didn't return a value for that function.
__________________
Perhaps I should have a sticky topic for all of the times I "return" to this forum instead of a new one every time.
Indigno is offline   Reply With Quote
Old Nov 14th, 2006, 9:40 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Just comparing the strings isn't going to get the job done. To locate a substring (if present), use strwhatever.find (). Additionally, your str2.compare would have covered your str1 == str2 condition. You only need one of those, depending upon whether or not you were looking for just equality or a complete relationship (<, ==, >).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 14th, 2006, 9:47 PM   #4
Indigno
Professional Programmer
 
Indigno's Avatar
 
Join Date: Dec 2005
Location: Anywhere non-productive
Posts: 267
Rep Power: 0 Indigno is an unknown quantity at this point
Send a message via AIM to Indigno Send a message via MSN to Indigno Send a message via Yahoo to Indigno
I've never really used String. I've always used APString.
__________________
Perhaps I should have a sticky topic for all of the times I "return" to this forum instead of a new one every time.
Indigno is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting strings into other strings codylee270 C 2 Dec 3rd, 2005 10:37 PM
Question about multidimensional arrays of strings aznluvsmc C 8 Oct 15th, 2005 11:20 PM
Comparing arguments to strings layer C++ 2 May 7th, 2005 3:39 PM
Comparing strings.... balltheheed Java 3 Apr 8th, 2005 4:26 AM
Comparing strings problem gencor45 C# 13 Feb 18th, 2005 6:57 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC