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.

C Strings User-defined Functions Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.