Home » C programs » C String User Defined Functions

C program to compare two string using case and ignoring case without using library function



In this program, we will learn how to compare two strings without using library function using case and ignoring case sensitivity.

Here, we will read two string (that can contains spaces too) and compares them using following two function,

  1. stringCmp() - Compares two strings (case sensitive).
  2. stringCmpi() - Compares two string ignoring case.

Program to compare two strings without using library function in C

#include <stdio.h>
#include <ctype.h>
 
 
/********************************************************
    *   function name       :stringCmp, stringCmpi
    *   Parameter           :char* s1,char* s2
    *   Return              :0- success, 1- fail
    *   Description         
        stringCmp   - compares two strings
        stringCmpi  - compares two string (ignoring case)
********************************************************/
int stringCmp (char *s1,char *s2);
int stringCmpi(char *s1,char *s2);
 
int main()
{
    char str1[100],str2[100];
 
	printf("Enter  string 1 : ");
    scanf("%[^\n]s",str1);//read string with spaces

    getchar(); //to read enter after first string
 
    printf("Enter  string 2 : ");
    scanf("%[^\n]s",str2);//read string with spaces
 
    if(!stringCmp(str1,str2))
        printf("\n stringCmp  :String are same.");
    else
        printf("\n stringCmp  :String are not same.");
 
    if(!stringCmpi(str1,str2))
        printf("\n stringCmpi :String are same.");
    else
        printf("\n stringCmpi :String are not same.");
 
 
    printf("\n");
    return 0;
}
 
/******** function definition *******/
int stringCmp (char *s1,char *s2)
{
    int i=0;
    for(i=0; s1[i]!='\0'; i++)
    {
        if(s1[i]!=s2[i])
            return 1;
    }
    return 0;
}
 
/******** function definition *******/
int stringCmpi (char *s1,char *s2)
{
    int i=0,diff=0;
    for(i=0; s1[i]!='\0'; i++)
    {
        if( toupper(s1[i])!=toupper(s2[i]) )
            return 1;           
    }
    return 0;
}

Output

Enter  string 1 : includehelp
Enter  string 2 : IncludeHelp

stringCmp  :String are not same.
stringCmpi :String are same.





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.