The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

stricmp() In C

Purpose of stricmp()

stricmp() is one of the inbuilt string function in c programming which is used to compare two strings without any discrimination between uppercase and lowercase letters, if the strings are same, it returns 0. Otherwise it returns a nonzero value.

How stricmp() Works

The following diagram clearly illustrate the working principle of stricmp() inbuilt string function in C.

stricmp() in c

In the above diagram stricmp() takes two parameters say str1 and str2. Here stricmp() compares two strings with case insensitive.

Syntax - stricmp()

  • stricmp() accepts two parameters.
  • Both parameters must be a string.
  • To use stricmp() inbuilt string function in C, we need to declare #include<string.h> header file.
Syntax
stricmp(str1, str2);

C Program - stricmp()

Let us work through stricmp() function. In the following program we will compare two string compare two strings without any discrimination between uppercases and lowercases by using stricmp() inbuilt string function.

c-using-stricmp.c
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20] = "this is stricmp";
char str2[20] = "THIS IS STRICMP";
int n;
n = stricmp(str1, str2);
if( n == 0)
printf("The strings str1 and str2 are same ");
else if(n == -1)
printf("The string str1 is lesser than str2");
else
printf("The string str1 is greater than str2");
return 0;
}
The strings str1 and str2 are same

Note:

The above program defines the function stricmp(), which is used to compare two strings without any discrimination between uppercase and lowercase letters.

C Program - Without stricmp()

Let us compare two strings irrespective of uppercases and lowercases without using any inbuilt string functions.

c-without-stricmp.c
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20] = "this is stricmp";
char str2[20] = "ThIs Is StRiCmP";
int i, j=0;
for(i=0; str1[i]!='\0'; i++){
// to find str1 length
}
for(j=0; str2[j]!='\0'; j++){
// to fine str2 length
}
if(i == j || i>j)
{
i = 0;
j = 0;
for(i=0; str1[i]!='\0'; i++){
if(str1[i] - str2[i] == 32 || str1[i] - str2[i] == -32 || str1[i] - str2[i] == 0)
j++;
else if(str1[i]<str2[i])
{
printf("The string str1 is lesser than str2");
return 0;
}
else {
printf("The string str1 is greater than str2");
return 0;
}
}
}
else
{
i = 0;
j = 0;
for(i=0; str2[i]!='\0'; i++){
if(str1[i] - str2[i] == 32 || str1[i] - str2[i] == -32 || str1[i] - str2[i] == 0)
j++;
else if(str1[i]<str2[i])
{
printf("The string str1 is lesser than str2");
return 0;
}
else {
printf("The string str1 is greater than str2");
return 0;
}
}
}
printf("The strings str1 and str2 are same ");
return 0;
}
The strings str1 and str2 are same

Note:

The above program looks verbose but yields the same result.

stricmp() table

Type Return Value
str1 > str2. 1
str1 == str2. 0
str1 < str2. -1

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report