C Program To Compare Two Strings

The following is a C Program To Compare Two Strings using strcmp on screen after taking input from the user. Let's get started.

C Program To Compare Two Strings using strcmp
C Program To Compare Two Strings using strcmp

Overview:


Commands

gets() function is used to obtain input as a string and printf() function is used to print the string on the screen.
strcmp(a,b) is a function used for comparing two strings and if result returned is 0, then strings are identical. Result is not 0 when unlike strings are entered.

Program

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
    char first[100],second[100];
    clrscr();
    printf("Enter the first string\n"); 
    gets(first);
    printf("Enter the second string\n");
    gets(second);
    if(strcmp(first,second)==0)
        printf("Entered strings are equal.\n");
    else
        printf("Entered strings are not equal.\n");
    getch();
}


Output

String Comparison Using strcmp
String Comparison Using strcmp

All programs compiled in this post are compiled in AnDOSBox.

Books I Prefer:






I hope this article was helpful to understand C Program To Compare Two Strings using strcmp! Comment below, if you've got any question. Head back soon for another interesting article on C Programming.

0 Comments

Post a Comment

Read Comment Policy before commenting.