Program to compare two strings

Program to compare two strings

21 April 2023

21 April 2023

Write a Program to compare two strings



If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get two strings as input and then compare that strings.

 

Input

Enter a string

Hi

Enter another string

Hello

 

Output

Strings are not equal

 

C Program

#include<stdio.h>

#include<string.h>

int main()

{

    char str1[20]={0};

    char str2[20]={0};

    printf("Enter a string: ");

    scanf("%s",str1);

    printf("Enter another string: ");

    scanf("%s",str2);

    int val = strcmp(str1,str2);

    if(val==0)

    printf("Strings are equal");

    else

    printf("Strings are not equal");

    return 0;

}

 

C++ Program

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

    char str1[20]={0};

    char str2[20]={0};

    cout<<"Enter a string: ";

    cin>>str1;

    cout<<"Enter another string: ";

    cin>>str2;

    int val = strcmp(str1,str2);

    if(val==0)

    cout<<"Strings are equal";

    else

    cout<<"Strings are not equal";

    return 0;

}

 

Java

import java.util.Scanner;

public class Main

{

              public static void main(String[] args) {

                  Scanner sc = new Scanner(System.in);

                             System.out.println("Enter a string: ");

                             String str1 = sc.nextLine();

                             System.out.println("Enter next string: ");

                             String str2 = sc.nextLine();

                             System.out.println(str1.equals(str2));

              }

}

 

Python

s1=input("Enter a string: ")

s2=input("Enter a string: ")

print(s1==s2)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023


Related Articles

Ask Us Anything !