Write a Program That Uses Function Strcmp to Compare Two Strings Input by the User
C++ Program to Compare Two Strings
In this article, you will learn and get code on comparing of two strings in C++. The program is created in following ways:
- Compare Two Strings without using strcmp() Function
- Using strcmp() Function
Compare Two Strings without strcmp()
To compare two strings in C++ programming, you have to ask from user to enter the two strings and compare them without using any type of library function like strcmp() as shown in the program given below. Let's have a look at the program first, will get the explanation later on:
#include<iostream> using namespace std; int main() { char str1[50], str2[50]; int i=0, chk=0; cout<<"Enter the First String: "; cin>>str1; cout<<"Enter the Second String: "; cin>>str2; while(str1[i]!='\0' || str2[i]!='\0') { if(str1[i]!=str2[i]) { chk = 1; break; } i++; } if(chk==0) cout<<"\nStrings are Equal"; else cout<<"\nStrings are not Equal"; cout<<endl; return 0; }
This program was build and run under Code::Blocks IDE. Here is its sample run:
Now enter the value of first string say codescracker and then again enter the value of second string say codescracker. Press ENTER
key to see the output as shown in the snapshot given below:
Here is another sample run, with user input codescracker as first string and crackercodes as second string:
And here is the last sample run with user input code as first string and codes as second input:
When user enters first string, then it gets initialized to str1 in a way that (supposing user enters code as first string):
- First character (c) gets initialized to str1[0]
- Second character (o) gets initialized to str1[1]
- Similarly str1[2]=d, str1[3]=e
- Then a null terminated character \0 automatically assigned after the last character of entered string, so str[4]=\0
In similar way, the second string gets initialized to str2. Now with user input, code and codes as first and second string, the dry run of above program goes like:
- Inside the while loop, the condition:
str1[i]!='\0' || str2[i]!='\0'
since initially i=0, therefore after replacing i with 0, the condition becomes
str1[0]!='\0' || str2[0]!='\0'
on putting the character present at 0th index of both the string
c!='\0' || c!='\0'
then the condition evaluates to be true. - Because neither first character of first string nor first character of second string is equal to null terminated character \0
- Therefore program flow goes inside the loop and evaluates the condition of if statement, that is
str1[i]!=str2[i]
or
c!=c
evaluates to be false. Therefore program flow does not goes inside the if's body, rather increments the value of i and goes back to the condition of while loop again - Because i=1 now, therefore proceed the same process using the updated value of i
- Process continues until any of the two condition inside while loop evaluates to be false. Or if the condition of if (inside the loop) evaluates to be true, therefore program flow goes inside the if's body and initializes 1 to chk, then using break keyword, program flow quit the while loop for further execution
- After exiting from the while loop (either by evaluating its condition as false, or by evaluating the if's condition as true), check for the value of chk in a way that, if it holds its initial value (0), then program flow never goes inside the if's body. That means, no any character at same index (of both string) mismatched. It means, string are equal. Otherwise if any mismatched found, then strings are not equal
What if User enters Strings of Unequal length ?
As you can see the output with two strings code and codes, the while loop runs 4 times (comparing code, first 4 character of first string, with code, first 4 character of second string) without any reason. Because if the length of two strings are not equal, then it is surely that strings can not be same.
So we do not need to evaluate while loop in that case. Therefore the program given below first finds the length of string and then checks whether length of both strings are equal or not. If it is equal, then will proceed to compare the strings. Otherwise print a message like unequal length or anything else (depends on you):
#include<iostream> using namespace std; int main() { char str1[50], str2[50]; int i, chk=0, len1=0, len2=0; cout<<"Enter the First String: "; cin>>str1; cout<<"Enter the Second String: "; cin>>str2; i=0; while(str1[i]!='\0') { len1++; i++; } i=0; while(str2[i]!='\0') { len2++; i++; } if(len1==len2) { i=0; while(str1[i]!='\0' || str2[i]!='\0') { if(str1[i]!=str2[i]) { chk = 1; break; } i++; } if(chk==0) cout<<"\nStrings are Equal"; else cout<<"\nStrings are not Equal"; } else cout<<"\nString Length must has to be same, to Compare!"; cout<<endl; return 0; }
Here is its sample run with user input code and codes as first and second string:
Compare Two Strings using strcmp()
With the help of library functions, the program becomes smaller. Because we do not need to write some extra code like finding length of string or comparing string, that can easily be processed with the help of library function like strlen() or strcmp().
The function, strcmp() takes two string as argument and returns 0 if both strings are equal. And the function strlen() takes single string as argument and returns its length.
#include<iostream> #include<string.h> using namespace std; int main() { char str1[50], str2[50]; int len1, len2; cout<<"Enter the First String: "; cin>>str1; cout<<"Enter the Second String: "; cin>>str2; len1 = strlen(str1); len2 = strlen(str2); if(len1==len2) { if(strcmp(str1, str2)==0) cout<<"\nStrings are Equal"; else cout<<"\nStrings are not Equal"; } else cout<<"\nStrings are not Equal"; cout<<endl; return 0; }
Here is its sample run with user input codescracker as first and second string both:
Same Program in Other Languages
- C Compare two Strings
- Java Compare two Strings
- Python Compare two Strings
C++ Online Test
« Previous Program Next Program »
Write a Program That Uses Function Strcmp to Compare Two Strings Input by the User
Source: https://codescracker.com/cpp/program/cpp-program-compare-two-string.htm
0 Response to "Write a Program That Uses Function Strcmp to Compare Two Strings Input by the User"
Postar um comentário