20/03/2016

Write a C Program to Compare Two Strings Using Pointers

C Program to Compare Two Strings Using Pointers. C program compares two strings without using a library function or string compares in c without using a library function. we are comparing two strings using pointers for comparing two strings we are not using an strcmp function we are comparing with the help of a pointer.

Write a C Program to Compare Two Strings Using Pointers

Explanation: In this problem,  First take input from the user first take a first string and take a second string and assign both strings in two pointer type variables separately, after that run a while loop and put the condition that string1 and string2 are the same and also both strings are not equal null till increase both string by one character and repeat the process again and again. If the first string is the same as a second string then print the message "Both Strings Are Same" if not then print "Both Strings Is Not Same" on the screen.

Recommended: If this post is beneficial for you and you want Updates for New posts then please like our Facebook Page or Facebook Group. If you have any queries you can ask them there with lots of coders also suggest your Friends join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.

C Program to Compare Two Strings Using Pointers


#include <iostream>
using namespace std;

int main()
{
    char string1[50],string2[50],*str1,*str2;
    int i,equal = 0;

    printf("Enter The First String: ");
    scanf("%s",string1);

    printf("Enter The Second String: ");
    scanf("%s",string2);

    str1 = string1;
    str2 = string2;

    while(*str1 == *str2)
    {
        if ( *str1 == '\0' || *str2 == '\0' )
            break;

        str1++;
        str2++;
    }

    if( *str1 == '\0' && *str2 == '\0' )
        printf("\n\nBoth Strings Are Equal.");
    else
        printf("\n\nBoth Strings Are Not Equal.");
}

Output Compare Two Strings Using Pointers in C

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: