Strcmp() function example program in C C++

Coding in C

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[20],str2[20];
    int dif;
    printf("Enter string1 : ");
    scanf("%s",&str1);
    printf("Enter string2 : ");
    scanf("%s",&str2);
    dif=strcmp(str1,str2);
    if(dif==0)
        printf("Same string");
    else
        printf("Not a same string");
    return 0;
}

Coding in C++

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str1[20],str2[20];
    int dif;
    cout<<"Enter string1 : ";
    cin>>str1;
    cout<<"Enter string2 : ";
    cin>>str2;
    dif=strcmp(str1,str2);
    if(dif==0)
        cout<<"Same string";
    else
        cout<<"Not a same string";
    return 0;
}

Output

Strcmp() function example program in C C++

Published by

admin

I am a computer teacher, programmer and web developer