![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2006
Posts: 1
Rep Power: 0
![]() |
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 ); } } |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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; string compare( string str1, string str2)
{
if (str1 == str2 )
{
return 0;
}
else
{
str2.compare( str1 );
}
}
__________________
Perhaps I should have a sticky topic for all of the times I "return" to this forum instead of a new one every time. |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Professional Programmer
|
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |