4.1 Check if two strings are the same in C++
4. Basic String Algorithm Problems in C++
Full Course on Data Structures and Algorithms in C++
By: Chrysanthus Date Published: 3 Feb 2026
The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.
Problem
Given two strings, s1 and s2, check if the two strings are identical(same) or not. If the strings are identical, print "Yes", otherwise, print "No". Consider case sensitivity. Employ O(n) time and O(n) space.
Examples:
Input: s1 = "abcd", s2 = "abcd" Output: Yes Input: s1 = "", s2 = "" Output: Yes Input: s1 = "Broad-network.com", s2 = "moc.krowten-daorB" Output: No Input: s1 = "Broad", s2 = "Broad-network.com" Output: No
Note:
Two empty strings are the same. An empty string should not have the space character. If the same text are in both strings and one is not in the same order as the other one, then both strings are not the same. The strings must be the same in number of characters, order of characters, and same casing for both strings.
Strategy
Compare the lengths of the strings, the characters and their order, and their casings.
Note: do not compare the variables of the two strings. Comparing their variables means comparing their references or pointers, which will hardly be the same.
Also note that since the two different cases (upper and lower) of the same character, are represented by different numbers in the computer, the program does not need extra coding to check the casing.
Program
The following program does the comparison (read through the code and comments):
#include <iostream>
using namespace std;
int areStringsSame(const char *sx, const char *sy, int Nx, int Ny) {
// Compare lengths first
if (Nx != Ny)
return 0;
// Compare character by character
int shorterLength = 0;
if (Nx < Ny)
shorterLength = Nx;
else
shorterLength = Ny;
for (int i = 0; i < shorterLength; ++i) { //avoid accessing out of bounds
if (sx[i] != sy[i]) {
return 0;
}
}
return 1; //in case the strings are the same
}
int main() {
int N1 = 0; //for length of s1
char s1[] = "Broad-network.com";
char *ptr1 = s1;
while (*ptr1 != '\0') { //in Cpp a string ends with '\0', which should not be counted
ptr1++;
N1 += 1;
}
int N2 = 0; //for length of s2
char s2[] = "Broad-network.com";
char *ptr2 = s2;
while (*ptr2 != '\0') {
ptr2++;
N2 += 1;
}
//call function and print answer
if (areStringsSame(s1, s2, N1, N2)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
int Na = 0; //for length of sa
char sa[] = "Broad";
char *ptrA = sa;
while (*ptrA != '\0') { //in Cpp a string ends with '\0', which should not be counted
ptrA++;
Na += 1;
}
int Nb = 0; //for length of sb
char sb[] = "Broad-network.com";
char *ptrB = sb;
while (*ptrB != '\0') {
ptrB++;
Nb += 1;
}
//call function and print answer
if (areStringsSame(sa, sb, Na, Nb)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
The output is:
Yes
No
The time complexity is actually O(2N) for the input and function for-loops; and the space complexity is actually O(2N) for the two strings. Since the coefficient (multiplier) is normally omitted, either time complexity is given by O(N).
The time and space complexities for the actual comparing operations of the lengths and characters, of the two strings, and possible temporary variables, are ignored.
Thanks for reading.
Related Links
More Related LinksCousins
BACK NEXTComments
Note: You can use the Search Box above to find articles and discussions of interest.