Javatpoint Logo
Javatpoint Logo

C program to compare the two strings

Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with the help of string function, i.e., strcmp(), which is defined in a string.h header file.

String comparison by using string function

The string function which is pre-defined in a string.h header file is a strcmp() function. The strcmp() function consider two strings as a parameter, and this function returns an integer value where the integer value can be zero, positive or negative.

The syntax of the strcmp() function is given below:

In the above syntax, two parameters are passed as strings, i.e., str1 and str2, and the return type is int means that the strcmp() returns an integer value.

The strcmp() function compares the character of both the strings. If the first character of both the strings are same, then this process of comparison will continue until all the characters are compared or the pointer points to the null character '\0'.

Possible return values from the strcmp() function

Return value Description
0 When both the strings are equal.
<0 If the ASCII value of a character of the first string is less than the ASCII value of a character of the second string, then the function will return negative value.
>0 If the ASCII value of a character of the first string is greater than the ASCII value of a character of the second string, then the function will return positive value.

Let's understand through an example.

Analysis of the above program

  • We have declared two arrays of char type, i.e., str1 and str2. We take the user input as strings.
  • We compare the strings by using the strcmp() function, i.e., strcmp(str1,str2). This function will compare both the strings str1 and str2. If the function returns 0 value means that both the strings are same, otherwise the strings are not equal.

Output:

C program to compare the two strings
C program to compare the two strings

String comparison without using strcmp() function

Analysis of the above program

  • In the above, we have declared two arrays of char type, and we take the user input as strings.
  • We have defined a compare() function which will take user input strings as a parameter, and compare both the strings. If the function returns 0, which means that both the strings are equal otherwise both the strings are not equal.

Output:

C program to compare the two strings

String comparison by using pointers

Analysis of the above program

  • We have created two arrays of char type str1 and str2. We take the user input as strings.
  • We have defined a stringcompare() function which will take two pointers of char type as a parameter. The 'a' pointer holds the address of str1 and 'b' pointer holds the address of str2. Inside the function, we have created a while loop which will execute until the pointer a or b is not reached to a null character.

Output:

C program to compare the two strings



Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA