Site icon Codeforcoding

strcmp string function in C programming Language

strcmp string function in C programming Language

We will learn about strcmp string function in C programming Language

Description

strcmp() – In the C Programming Language, the strcmp function is a comparison operator. It  compares two Strings and displays an integer value returns either negative, positive integer or zero

 

Declaration

Syntax for strstr() function is given below

int strcmp(const char *str1, const char *str2);

Parameter or arguments

str1 – This is the first string to be compared

str2 – This is the second string to be compared

 

Returns

strcmp is a predefined string function in C language used to compare two strings. This function returns either negative, positive integer  or zero

if Returns value=0, then it denoted String 1 is same as String 2.

if Returns value<0, then it denoted String 1 is less than String 2.

if Returns value>0 , then it denoted String 1 is greater than String 2.

 

Required Header

the required header for strcmp() String function, in C Programming language, is:

#include<string.h>

strcmp string function in C

Example

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char first[100], second[100];
    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 same\n");
    else
        printf("Entered strings are not same\n");
        getch();
    return 0;
}

When above program me  is compiled it will produce the following result

you have entered same string – output here

Enter the first string
hello
Enter the second string
hello
Entered strings are same

 

you have entered different string – output here

Enter the first string
hello
Enter the second string
world
Entered strings are not same

 

   

There are other C programming language functions that are  similar to this function

strncmp() function in C language

strcat() function in C language

strcpy() function in C language

strncpy() function in C language

strset() function in C language

strlwr() function in C language

strdub() function in C language

strupr() function in C language

strlen()  function in C language

strnset() function in C language

strchr()  function in C language

strrchr()  function in C language

strtok()  function in C language

strrev()  function in C language

 

Suggested for you

String function in C Language

Function in C language

Arithmetic function in C language

User defined function in C language

function in python language

recursive Function in C language

recursive Function in C++ language

recursive Function in Python language

 

 

 

 

strstr string function in C programming language
Arithmetic function in C programming language
Exit mobile version