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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

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 …