Comparing two strings without functions that do it for you

I am definitely a rookie in all of this. My C++ teacher has instructed us to create a program where the user inputs two sentences. Those sentences are stored as character arrays and then compared to see if they are the same or different. We have to do this without the functions like compare and substr. I am lost and don't know how to do any of this. I don't think I did any of this right, but the code is below. Thank you all

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string sone, stwo;
	cout << "Enter a sentence:" << endl;
	getline(cin, sone);
	cout << "Enter another sentence:" << endl;
	getline(cin, stwo);
	if (sone == stwo)
	{
		cout << "These are the same sentences." << endl;
	}
	else
	{
		cout << "These sentences are different." << endl;
	}
}
Last edited on
Well, first, you should use character arrays, as instructed. You'll have to remember or look at your notes on arrays. (If you didn't pay attention for that, you'll have to read about them in your book or online.)

Next, you'll need a loop to compare the elements of the two arrays pairwise, stopping when you encounter a 0 in either array.

If both arrays have the same characters pairwise, up to and including the zeros terminating the strings, then they are equal.

Otherwise they are not.


Make sure you make yourself a big enough array to store a sentence, and that you specify a maximum amount of characters to read:

1
2
3
4
char s1[1000];
...

cin.getline( s1, 1000 );

Hope this helps.
Thank you for that, it did help. Well, I tried this, but it always says the sentences are the same.

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

int main()
{
	char s1[1000], s2[1000];
	cout << "Insert a sentence:" << endl;
	cin.getline(s1, 1000);
	cout << "Insert another sentence:" << endl;
	cin.getline(s2, 1000);
	for (int i = 0; i < 1000; i++)
	{
		if (s1[i] = s2[i])
		{
			cout << "These sentences are the same." << endl;
			return 0;
		}
		else if (s1[i] != s2[i])
		{
			cout << "These sentences are not the same." << endl;
			return 0;
		}
	}
}
Last edited on
line 14:
if (s1[i] = s2[i])

By using the operator =, you are assigning the value s2[i] to s1[i]. Therefore, the statement will be true.

if you change line 14 to:
if (s1[i] == s2[i])

then, you will be comparing the values between s2[1] and s1[i]. I hope it helps.
I see what you're saying and changed that, however, it is still saying all sentences are the same. Can't figure this out.
When a return statement is reached, the current function is terminated. Remove the return statements from the body of your loop.

On line 14 you are comparing a single element of s1 to a single element of s2. Do you think comparing a single element of each is enough to say the sentences are the same?
Thank you, however if I remove the return statements, it prints "These sentences are the same" over and over again. I'm sorry if this is elementary, I'm just having a lot of trouble understanding.
Last edited on
On line 14 you are comparing a single element of s1 to a single element of s2. Do you think comparing a single element of each is enough to say the sentences are the same?

This is what I did. I know it is supposed to consider all the characters in the sentence, but I'm not sure what operation is used for that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<string>
using namespace std;

int main()
{
	char s1[1000], s2[1000];
	cout << "Insert a sentence:" << endl;
	cin.getline(s1, 1000);
	cout << "Insert another sentence:" << endl;
	cin.getline(s2, 1000);
	for (int i = 0; i < 1000; i++)
	{
		if (s1[i] == s2[i])  
		{
			cout << "These sentences are the same." << endl;
		}
		else if (s1[i] != s2[i])
		{
			cout << "These sentences are not the same." << endl;
		}
	}
}
Thank you, however if I remove the return statements, it prints "These sentences are the same" over and over again.

This is because your for loop iterates through each character.
1
2
char s1[] = "Hello World!";
char s2[] = "Hello Earth!";

This is what your program is doing.
Starts at index 0, which is the first character in the char array.
Since 'H' is equal to 'H' (first letter in s1 and s2, provided above) it outputs "These sentences are the same." Once the characters are not equal, that is, at index 6 ('W' is not equal to 'E') it outputs "These sentences are not the same."

I think you need something called a flag. bool same = false;
If the characters are the same, then set same to true, else set same to false and break out of the loop. From that, you can use a switch or if/else statement on same and print out what you want from there.

This probably isn't the best way to do it, but if someone has a better way, feel free to suggest it. :)

Also, I would recommend you use strlen or use a loop to calculate the number of chars in the sentence the user enters. Since you've allocated space for 1000 chars (including null terminator) and the chances of the user typing out 1000 chars in a sentence is unlikely, the for loop would iterate through junk memory (I'm still learning C++ so I don't know if this is right) which would result in random results. This is not what you want; you want to loop through the characters the user enters and not unused junk memory.
Last edited on
Plan the algorithm before you write the code.

let i = 0
while s1[i] is equal to s2[i] and s1[i] is non-zero, increase i

if s1[i] is equal to s2[i], then the strings are the same
else, they aren't.
Thank you! This worked.
Topic archived. No new replies allowed.