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 = “GeeksforGeeks”, string2 = “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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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("%s", string1);
    printf("Enter the first string: %s", string1);
  
    // Get the strings which
    // is to be checked
    scanf("%s", 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;
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

Enter the first string: GeeksForGeeks
Enter the second string: GeeksForGeeks
Are both strings same: Yes


My Personal Notes arrow_drop_up

Technical Content Engineer

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.



Improved By : Ryuga