C++ Program to Compare Two Strings2 min read

This is the C++ tutorial where we will write a C++ program to compare two strings. If you want to learn more about string in C++, click the link below.

We will compare two strings in two ways:

  1. Compare using strcmp() Function
  2. Compare two strings without using strcmp() function

C++ Program to Compare Two Strings

Both of the programs below take user input for the two strings that are to compare. Although to take input from the user you can use gets(str) function that will take the string input with spaces in it.

The following program uses cin>> method to take a string input. It only takes a single word.

1. C++ Program to compare two strings without using strcmp()

This program compare string in C++ without using strcmp() function. We sill use while loop to iterate through the string and inside while loop if loop is used to see if every letter is equal or not on both of the string.

If it is not equal then the if statement is executed and a flag variable will raise to 1 and the execution control comes out of the loop with the use of break statement.

Output:

Enter the first string: simple2code
Enter the second string: simple2code

Strings are Equal.


2. C++ Program to compare two strings using strcmp()

The following program compare string in C++ using strcmp() function that is provided in the C++ library. This inbuilt function is defined under string.h header file. Hence, it is necessary to include this file in the program at the beginning.

Output:

Enter the first string: coding
Enter the second string: coding

Strings are Equal.

Enter the first string: coding
Enter the second string: Coding

Strings are not Equal.

As you can see, the comparison on both of the programs are also case sensitive, the word “coding” is not equal to the word “coding”. Since the first letter is a lower letter in one string and capital on another.


MORE

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More