Problem statement:- Program to Check whether two strings are the same or not
Data requirement:-
Input Data:- str, str2
Output Data:- Nothing
Additional Data:-i, count
Program in C
Here is the source code of the C Program to Check whether two strings are the same or not.
Output Data:- Nothing
Additional Data:-i, count
Here is the source code of the C Program to Check whether two strings are the same 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("Strings characters are not same.");
else
{
int i,count=0;
for(i=0;i<strlen(str);i++)
{
if(str[i]==str2[i]){
count=1;
break;
}}
if(count!=1)
{
printf("Strings characters are not same.");
}
else
printf("Strings characters are the same.");
}}
#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("Strings characters are not same.");
else
{
int i,count=0;
for(i=0;i<strlen(str);i++)
{
if(str[i]==str2[i]){
count=1;
break;
}}
if(count!=1)
{
printf("Strings characters are not same.");
}
else
printf("Strings characters are the same.");
}}
Input/Output:
Enter your 1st String:csinfo
Enter your 2nd String:csinfo
Strings characters are the same.
Program in C++
Here is the source code of the C++ Program to Check whether two strings are the same or not.
Enter your 2nd String:csinfo
Strings characters are the same.
Here is the source code of the C++ Program to Check whether two strings are the same or not.
Code:
#include<iostream>
using namespace std;
int main()
{
string str,str2;
cout<<"Enter your 1st String:";
getline(cin,str);
cout<<"Enter your 2nd String:";
getline(cin,str2);
int len=0,len2=0,i;
while(str[len]!='\0')
{
len++;
}
while(str2[len2]!='\0')
{
len2++;
}
if(len!=len2)
cout<<"Strings character are not the same.";
else
{
int i,count=0;
for(i=0;i<len;i++)
{
if(str[i]==str2[i]){
count=1;
break;
}}
if(count!=1)
{
cout<<"Strings character are not the same.";
}
else
cout<<"Strings character are the same.";
}}
using namespace std;
int main()
{
string str,str2;
cout<<"Enter your 1st String:";
getline(cin,str);
cout<<"Enter your 2nd String:";
getline(cin,str2);
int len=0,len2=0,i;
while(str[len]!='\0')
{
len++;
}
while(str2[len2]!='\0')
{
len2++;
}
if(len!=len2)
cout<<"Strings character are not the same.";
else
{
int i,count=0;
for(i=0;i<len;i++)
{
if(str[i]==str2[i]){
count=1;
break;
}}
if(count!=1)
{
cout<<"Strings character are not the same.";
}
else
cout<<"Strings character are the same.";
}}
Input/Output:
Enter your 1st String:csinfo
Enter your 2nd String:dotcom
Strings characters are not the same.
Enter your 2nd String:dotcom
Strings characters are not the same.
Here is the source code of the Java Program to Check whether two strings are the same or not.
Code:
import java.util.Scanner;
public class P31 {
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.length()!=str2.length())
System.out.println("Strings are not the same.");
else
{
int i,count=0;
char[] str_1=str1.toLowerCase().toCharArray();
char[] str_2=str2.toLowerCase().toCharArray();
for(i=0;i<str1.length();i++)
{
if(str_1[i]==str_2[i]){
count=1;
break;
}}
if(count!=1)
System.out.println("Input strings are not the same.");
else
System.out.println("Input strings are the same.");
}
cs.close();
}
}
public class P31 {
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.length()!=str2.length())
System.out.println("Strings are not the same.");
else
{
int i,count=0;
char[] str_1=str1.toLowerCase().toCharArray();
char[] str_2=str2.toLowerCase().toCharArray();
for(i=0;i<str1.length();i++)
{
if(str_1[i]==str_2[i]){
count=1;
break;
}}
if(count!=1)
System.out.println("Input strings are not the same.");
else
System.out.println("Input strings are the same.");
}
cs.close();
}
}
Input/Output:
Enter your 1st String:
cat
Enter your 2nd String:
tac
Input strings are not the same.
Program in Python
Here is the source code of the Python Program to Check whether two strings are the same or not.
cat
Enter your 2nd String:
tac
Input strings are not the same.
Here is the source code of the Python Program to Check whether two strings are the same or not.
Code:
str=input("Enter the 1st String:")
str2=input("Enter the 2nd String:")
count = 0
if len(str) != len(str2):
print("Strings are not the same.")
else:
for i in range(0,len(str)):
if str[i] == str2[i]:
count=1
break
if count!=1:
print("Input strings are not the same.")
else:
print("Input strings are the same.")
str2=input("Enter the 2nd String:")
count = 0
if len(str) != len(str2):
print("Strings are not the same.")
else:
for i in range(0,len(str)):
if str[i] == str2[i]:
count=1
break
if count!=1:
print("Input strings are not the same.")
else:
print("Input strings are the same.")
Input/Output:
Enter the 1st String:info
Enter the 2nd String:info
Input strings are the same.
More:-
Enter the 2nd String:info
Input strings are the same.
More:-
0 Comments
Please do not Enter any spam link in the comment box