20_10

Write a C code to compare two strings.

 Source code:

/*Write a C code to compare two strings e.g. str1 and str2 and check whether strings are equal and str1 is greater than str2 or less, without using standard string library. */

#include <stdio.h>

#include <string.h>

int main(){

    char str1[20],str2[20];

    int c=0,d=0;

printf("Enter 1st string:");

    //Read String 1

    scanf("%s", &str1);

printf("Enter 2nd string:");

    //Read String 2

    scanf("%s", &str2);

int i=0;

while (str1[i] != '\0')

{

c++;

i++;

}

i=0;

while (str2[i] != '\0')

{

d++;

i++;

}

if (c==d)

{

        printf ("Strings are equal");

}

else if (c>d)

{

        printf ("String 1 is greater than String 2");

}

else

{

        printf ("String 1 is less than String 2");

}

return 0;

}

Output:
C code to compare two strings e.g. str1 and str2 and check whether strings are equal and str1 is greater than str2 or less, without using standard string library


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...