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.