Program to check if two strings are same or not
Given two strings, the task is to check if these two strings are identical(same) or not.
Examples:
Input: string1 = “GeeksforGeeks”, string2 = “GeeksforGeeks”
Output: Yes
Input: string1 = “Geeks for Geeks”, string2 = “Geeks for Geeks”
Output: YesInput: string1 = “GeeksforGeeks”, string2 = “Geeks”
Output: NoInput: string1 = “Geeks for Geeks”, string2 = “Geeks for geeks”
Output: No
Approach: This can be done with the help of strcmp() method in C. Please note, this method is case-sensitive.
Implementation:
C++
// C++ program to check if // two strings are identical #include <bits/stdc++.h> using namespace std; int main() { char string1[100], string2[100]; // Get the strings which // is to be checked cin >> string1; cout << "Enter the first string: " << string1; // Get the strings which // is to be checked cin >> string2; cout << "\nEnter the second string: " << string2; // Check if both strings are equal cout << "\nAre both strings same: "; if (strcmp(string1, string2) == 0) { cout << "Yes"; } else { cout << "No"; } return 0; } // This code is contributed by Akanksha Rai |
C
// C program to check if // two strings are identical #include <stdio.h> #include <string.h> int main() { char string1[100], string2[100]; // Get the strings which // is to be checked scanf("%[^\n]\ns", string1); printf("Enter the first string: %s", string1); // Get the strings which // is to be checked scanf("%[^\n]\ns", string2); printf("\nEnter the second string: %s", string2); // Check if both strings are equal printf("\nAre both strings same: "); if (strcmp(string1, string2) == 0) { printf("Yes"); } else { printf("No"); } return 0; } |
Java
// Java program to check if // two strings are identical import java.util.*; class GFG { // Driver code public static void main(String[] args) { Scanner in = new Scanner(System.in); // Get the strings which is to be checked String string1 = in.nextLine(); System.out.println("Enter the first string: " + string1); // Get the strings which is to be checked String string2 = in.nextLine(); System.out.println("Enter the second string :" + string2); // Check if both strings are equal System.out.println("\nAre both strings same: "); if (string1.equals(string2) == true) { System.out.println("Yes"); } else { System.out.println("No"); } } } // This code is contributed by aishwarya.27 |
Python 3
# Python program to check if # two strings are identical if __name__ == "__main__" : # Get the strings which # is to be checked string1 = input("Enter the first string: ") print(string1, end = "\n") # Get the strings which # is to be checked string2 = input("Enter the second string: ") print(string2, end ="\n") # Check if both strings are equal print("Are both strings same: ", end = " ") if (string1 == string2) : print("Yes") else : print("No") # This code is contributed by Ryuga |
C#
// C# program to check if // two strings are identical using System; class GFG { // Driver code public static void Main() { // Get the strings which is to be checked String string1 = Console.ReadLine(); Console.WriteLine("Enter the first string: " + string1); // Get the strings which is to be checked String string2 = Console.ReadLine(); Console.WriteLine("Enter the second string :" + string2); // Check if both strings are equal Console.WriteLine("\nAre both strings same: "); if (string1.Equals(string2) == true) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } /* This code contributed by PrinciRaj1992 */ |
PHP
<?php // PHP program to check if // two strings are identical // Get the strings which // is to be checked $string1 = readline(); echo "Enter the first string: $string1"; // Get the strings which // is to be checked $string2 = readline(); echo "\nEnter the second string: $string2"; // Check if both strings are equal echo "\nAre both strings same: "; if (strcmp($string1, $string2) == 0) { echo "Yes"; } else if (strcmp($string1, $string2) > 0) //Because string1 has some character greater than string2 { echo "No"; } else if (strcmp($string1, $string2) < 0) //Because string2 has some character greater than string1 { echo "No"; } // This code is contributed by Ashutosh Tiwari ?> |
Enter the first string: GeeksForGeeks Enter the second string: GeeksForGeeks Are both strings same: Yes
Recommended Posts:
- A Program to check if strings are rotations of each other or not
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if two strings are k-anagrams or not
- Check whether Strings are k distance apart or not
- Check whether two strings are anagram of each other
- Check if two given strings are isomorphic to each other
- Check if two strings are permutation of each other
- Check if strings are rotations of each other or not | Set 2
- Check if there is any common character in two given strings
- Check if two strings are same ignoring their cases
- Check duplicates in a stream of strings
- Check if all the palindromic sub-strings are of odd length
- Check whether two strings are equivalent or not according to given condition
- Check whether two strings contain same characters in same order
- Check if edit distance between two strings is one
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



