Write a function to which compares two strings to find out whether they are same or different. The two strings must be compared character by character until there is mismatch or end of one of the string is reached, whichever occurs first. If two strings are identical, function should return a value zero, otherwise one.
#include <iostream>
using namespace std;
int compareStrings(string s1,string s2){
int n1=s1.length();
int n2=s2.length();
if (n1!=n2){
return 1;
}
else{
int i=0;
while (i<n1){
if (s1[i]!=s2[i]){
break;
}
i++;
}
if (i == n1)
return 0;
else
return 1;
}
}
int main()
{
cout<<compareStrings("Hello","Hello");
return 0;
}
Comments
Leave a comment