Answer to Question #200807 in C++ for M Kamran

Question #200807

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.


1
Expert's answer
2021-05-30T13:35:37-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog