- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C Program to check if two strings are same or not
Given two strings str1 and str2 we have to check whether the two strings are same or not. Like we are given two stings “hello” and “hello” so they are identical and same.
Identical are the strings which seems equal but are not equal like : “Hello” and “hello”, and same are the strings which are exactly same like : “World” and “World”.
Example
Input: str1[] = {“Hello”}, str2[] = {“Hello”} Output: Yes 2 strings are same Input: str1[] = {“world”}, str2[] = {“World”} Output: No, 2 strings are not same
The approach used below is as follows −
We can use strcmp(string2, string1).
strcmp() string compare function is a in-built function of “string.h” header file, this function accepts two parameters, both strings. This function compares two strings and checks whether both strings are same and return 0 if there is no change in the string and return a non-zero value when the two strings are not same. This function is case-sensitive, means both the strings should be exactly same.
- So we will take two strings as an input.
- Use strcmp() and pass both the strings as parameters
- If they return zero then print “Yes 2 strings are same”
- Else print “No, 2 strings are not same”.
Algorithm
Start In function int main(int argc, char const *argv[]) Step 1-> Declare and initialize 2 strings string1[] and string2[] Step 2-> If strcmp(string1, string2) == 0 then, Print "Yes 2 strings are same
" Step 3-> else Print "No, 2 strings are not same
" Stop
Example
#include <stdio.h> #include <string.h> int main(int argc, char const *argv[]) { char string1[] = {"tutorials point"}; char string2[] = {"tutorials point"}; //using function strcmp() to compare the two strings if (strcmp(string1, string2) == 0) printf("Yes 2 strings are same
"); else printf("No, 2 strings are not same
" ); return 0; }
Output
If run the above code it will generate the following output −
Yes 2 strings are same