Comparing Two Strings (C++)

Objective:
Function that takes two strings as parameters & returns if true if same.

Right now, I have my code working to compare two strings lengths.

Inputs of abc & abc return true.
Inputs of abd & abd return true.

However, I do not want inputs of abc & abd return true like they do with my code, because these strings are not equal, right?

How would I fix my code for this?

Thank you so much!

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
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

bool isEqual(string foo, string fool)
{
	if (foo[0] != fool[0])
		{
	          return false;
		}
	else
	        return true;
}
	
	
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();

}
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
@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
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.
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
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
Thank you, but I need to use arrays I believe. & It's cheating to just say if x == y!
Last edited on
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
47
48
49

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

bool isEqual(string foo, string fool)
{
	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)
	{
		for (int i = 0; i < length && i < lengthTwo; i++)
		{
			if (foo [i] != fool [i])
			{
				return false;
			}

		}
		return true;
	}

}

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();

}


study this.
@CodeGoggles Wow, I was totally over complicating this. It all makes sense now. Thank you for your assistance and time!
ohh by the way I just noticed that you used for loops without a body that a manage to copy in lol.
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
If you are using,
std::string is not
cheating, but idk
if you are using c++
strings, why you attempt
to compare like C-style,
in that case you should use
C strings i.e.
1
2
3
char* str1="text";
//or
char str2[]="someText";


to be honest i am
confused what is your
goal, but you can compare
each element of 2 std::string
to verify if are equals;

@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
This is what i guess
you are thinking;



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
//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.size()!=str2.size())return false;

        for(int i=0;i<str1.size();i++)
                if(str1[i]!=str2[i])return false;


return true;
}//end function isEqual
Eyenrique-MacBook-Pro:Desktop Eyenrique$ ./compareString 
Enter string 1: C-Style!
Enter string 2: C-Style!
Equal!
Eyenrique-MacBook-Pro:Desktop Eyenrique$ ./compareString 
Enter string 1: Hello!
Enter string 2: Hey!
Not equal!
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.
@CodeGoggles Yea, you dont want to check the characters if the lengths arent equal in the first place haha. Thanks again.
Topic archived. No new replies allowed.