Apr 11, 2014 at 12:20pm UTC
Right now, I have my code working to compare two strings lengths.
No you do not.
Try these as your 2 input strings:
1. "abc"
2. "abxsdfdsfdsfdsfvdfsdfdsfds"
Your program above will tell me those 2 strings are equal.
That might give you a hint on what you're doing wrong and how to compare them exactly.
I assume you're not allowed to do something simple in your function like:
return (foo == fool) ? true : false ;
?
Last edited on Apr 11, 2014 at 12:24pm UTC
Apr 11, 2014 at 12:28pm UTC
@mutexe I'm confused because I swear it just said not equal for them. I must have changed something very small that I can't figure out. I honestly am not sure where to go from here.
And yes, I can't do that haha. I want to compare lengths of two, and if they have the same length, I then have to compare the character at each index. (If lengths are not same, it will just return false and not compare the characters because that is unnecessary).
Last edited on Apr 11, 2014 at 12:33pm UTC
Apr 11, 2014 at 12:31pm UTC
It might have to do with you comparing the first char in the string. As I don't see you looping over the rest of the character array.
Apr 11, 2014 at 12:38pm UTC
I think I figured out how to solve the length problem! (Right?)
Now, I assume we have the length situation figured out.
How would I compare the characters at each index?
Because, abc & abd should not return equal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
bool isEqual(string foo, string fool)
{
if (foo[0] == fool[0])
{
int length, lengthTwo;
length = foo.length();
for (int x = 0; x < length; x++)
lengthTwo = fool.length();
for (int y = 0; y < length; y++)
if (length == lengthTwo)
return true ;
}
else
return false ;
}
int main()
{
cout << "Enter Two Strings:" << endl;
string foo, fool;
cin >> foo >> fool;
isEqual(foo,fool);
if (isEqual(foo, fool) == true )
cout << "Equal!" ;
if (isEqual(foo, fool) != true )
cout << "Not Equal!" ;
cin.get();
cin.ignore();
}
Last edited on Apr 11, 2014 at 12:51pm UTC
Apr 11, 2014 at 12:45pm UTC
Hi @sportstool,
to compare 2 strings
(std::string)you can
do it directly, but
you can iterate through
the string if you want,
i have a little example
comparing 2 strings;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
//compareString.cpp
//##
#include <iostream>
#include <string>
using namespace std;
//Function prototype
bool isEqual(string str1,string str2);
int main(){
string str1,str2;
cout<<"Enter string 1: " ;
getline(cin,str1);
cout<<"Enter string 2: " ;
getline(cin,str2);
if (isEqual(str1,str2))
cout<<"Equal!" ;
else
cout<<"Not equal!" ;
cout<<endl;
return 0; //indicates success
}//end of main
//Function implementation
bool isEqual(string str1,string str2){
if (str1==str2)return true ;
return false ;
}//end function isEqual
Eyenrique-MacBook-Pro:Desktop Eyenrique$ ./compareString
Enter string 1: Hello World!
Enter string 2: Hello World!
Equal!
Eyenrique-MacBook-Pro:Desktop Eyenrique$ ./compareString
Enter string 1: Hello!
Enter string 2: Hey you!
Not equal!
Last edited on Apr 11, 2014 at 12:47pm UTC
Apr 11, 2014 at 12:48pm UTC
Thank you, but I need to use arrays I believe. & It's cheating to just say if x == y!
Last edited on Apr 11, 2014 at 12:51pm UTC
Apr 11, 2014 at 12:55pm UTC
@CodeGoggles Wow, I was totally over complicating this. It all makes sense now. Thank you for your assistance and time!
Apr 11, 2014 at 12:57pm UTC
ohh by the way I just noticed that you used for loops without a body that a manage to copy in lol.
Apr 11, 2014 at 12:58pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
bool isEqual(string foo, string fool)
{
int length, lengthTwo;
length = foo.length();
lengthTwo = fool.length();
if (length == lengthTwo)
{
for (int i = 0; i < length && i < lengthTwo; i++)
{
if (foo [i] != fool [i])
{
return false ;
}
}
return true ;
}
return false ;
}
int main()
{
cout << "Enter Two Strings:" << endl;
string foo, fool;
cin >> foo >> fool;
isEqual(foo, fool);
if (isEqual(foo, fool) == true )
cout << "Equal!" ;
if (isEqual(foo, fool) != true )
cout << "Not Equal!" ;
cin.get();
cin.ignore();
}
this is better
Last edited on Apr 11, 2014 at 1:05pm UTC
Apr 11, 2014 at 12:59pm UTC
@CodeGoggles One last thing. Is there a way to use arrays with this? I believe I was supposed to.
Ah wait, I just saw your edit. That's with arrays, right? I dont think any more usage of arrays could be put in here...
Last edited on Apr 11, 2014 at 1:00pm UTC
Apr 11, 2014 at 1:08pm UTC
basically you just use int I in the for loop to represent the number of the array index but check length first as you don't want to run off the end of the array. especially when writing values this can be dangerous.
Apr 11, 2014 at 1:38pm UTC
@CodeGoggles Yea, you dont want to check the characters if the lengths arent equal in the first place haha. Thanks again.