Starting Vocational Training From 1-May-2024 Get Detail


Write a program in C to compare two string without using string library functions. 

Test Data :
Input the 1st string : This is first string
Input the 2nd string : This is first string

Expected Output :

The length of both strings are equal and
also both strings are equal.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define str_size 100 //Declare the maximum size of the string


void main()
{
    char str1[str_size], str2[str_size];
    int flg=0;
    
       printf(" Compare two string whether they are equal or not : ");
       printf("------------------------------------------------------ ");     
       printf("Input the 1st string : ");
       fgets(str1, sizeof str1, stdin);    
       
       printf("Input the 2nd string : ");
       fgets(str2, sizeof str2, stdin);           
 
    int i=0;

    /* Runs till both strings are equal */
    while(str1[i] == str2[i])
    {
        if(str1[i] == || str2[i] == )
            break;

        i++;
    }
    if(str1[i-1] == && str2[i-1]==)
        flg=0;
    else if(str1[i] > str2[i])
        flg=1;
    else if(str1[i] < str2[i])
        flg=-1;
  
    if(flg == 0)
    {
        printf(" The length of both strings are equal and also both strings are equal. ");
    }
    else if(flg == -1)
    {
        printf(" The length of the first string is smaller than second. ");
    }
    else
    {
        printf(" The length of the first string is greater than second. ");
    }
}