Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to concatenate two strings without using... >>
<< C program to check whether a substring is present ...