C++ Program to Compare Two Strings Using Pointers

In beneath program, we are contrasting two string and the assistance of pointers, without

utilizing strcmp() work. A client characterized work str_cmp() is made which accepts

two character pointers as contention. On the off chance that this capacity returns 1

then the strings are equivalent and inconsistent if returns 0. Simply investigate

the program, in the event that you are confronting any issue to see, at that point don’t hesitate to ask by remarking underneath.

Program

#include<iostream>
#include<stdio.h>


using namespace std;


main()
{
    char str1[50],str2[50];
    int str_cmp(char*,char*);
    cout<<“Enter first string:”;
    gets(str1);
    cout<<“Enter second string:”;
    gets(str2);


    if(str_cmp(str1,str2))
        cout<<“nStrings are equal”;
    else
        cout<<“nStrings are not equal”;


    return 0;
}


int str_cmp(char *s1,char *s2)
{
    while(*s1==*s2)
    {
        if(*s1==’’||*s2==’’)
            break;


        s1++;
        s2++;
    }


    if(*s1==’’&&*s2==’’)
        return 1;


    return 0;
}

Output

C++ Program to Compare Two Strings Using Pointers

Leave a Comment

error: Alert: Content is protected!!