Tuesday, February 17, 2015

C Program to Compare Two Strings Using Pointers

C++ Program to Compare Two Strings Using Pointers

In below program we are comparing two string with the help of pointers, without using strcmp() function. A user defined function str_cmp() is created which takes two character pointers as argument. If this function returns 1 than the strings are equal and unequal if returns 0. Just take a look on the program, if you are facing any problem to understand then feel free to ask by commenting below.

Also Read: C++ Program to reverse a string
Also Read: C++ Program to Count no. of words in a string

#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<<"
Strings are equal";

    else
        cout<<"
Strings are not equal";


    return 0;
}

int str_cmp(char *s1,char *s2)
{
    while(*s1==*s2)
    {
        if(*s1==

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.