Check whether two strings are equal or not

Problem statement:- Program to Check whether two strings are equal or not.

Data requirement:-

   Input Data:- str, str2

  Output Data:-String Output

Program in C

Here is the source code of the C Program to Check whether two strings are equal or not.

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[30];
    printf("Enter your 1st String:");
    gets(str);
    char str2[30];
    printf("Enter your 2nd String:");
    gets(str2);
    if(strlen(str)==strlen(str2))
    printf("Two strings are equal.");
    else
    printf("Two strings are not equal.");
}

Input/Output:
Enter your 1st String:csinfo
Enter your 2nd String:dotcom
Two strings are equal.

Program in C++

Here is the source code of the C++ Program to Check whether two strings are equal or not.

Code:

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    string str;
    cout<<"Enter your 1st String:";
    getline(cin,str);
    int len=0;
    while(str[len]!='\0')
    {
        len++;
    }
    string str2;
    cout<<"Enter your 2nd String:";
    getline(cin,str2);
    int len2=0;
    while(str2[len2]!='\0')
    {
        len2++;
    }
    if(len==len2)
    cout<<"Two strings are equal.";
    else
    cout<<"Two strings are not equal.";
}

Input/Output:
Enter your 1st String:test string
Enter your 2nd String:string one
Two strings are not equal.

Program in Java

Here is the source code of the Java Program to Check whether two strings are equal or not.

Code:

import java.util.Scanner;
public class P11 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your 1st String:");
str1=cs.nextLine();
String str2;
System.out.println("Enter your 2nd String:");
str2=cs.nextLine();
if(str1.equals(str2))
System.out.print("Two strings are equal.");
    else
    System.out.print("Two strings are not equal.");
cs.close();
}
}

Input/Output:
Enter your 1st String:csinfo
Enter your 2nd String:dotcom
Two strings are equal.

Program in Python

Here is the source code of the Python Program to Check whether two strings are equal or not.

Code:

str=input("Enter the 1st String:")
str1=input("Enter the 2nd String:")
if(len(str)==len(str1)):
    print("Two strings are equal.")
else:
    print("Two strings are not equal.")

Input/Output:
Enter your 1st String:test string
Enter your 2nd String:string one
Two strings are not equal.

Post a Comment

0 Comments