C program to compare two string with or without strcmp


                                          To compare two strings with each other in C Programming there is one string function strcmp(). The following is the syntax of string copy function.

Syntax: 
                                        strcmp(string1,string2);

Example:
                                         strcmp(str1,str2);

                                          In strcmp() function it return some value if the both string are equal than it return 0 other wise it return negative value so after using string function we need to store value in some temporary integer variable. And than after we need to check condition if temp is Zero than both string are equal otherwise it not equal.


#include<stdio.h>

void main()
{
        char str1[50],str2[50];
        int temp;
        clrscr();

        printf("Enter First string : ");
        scanf("%s",str1);

        printf("Enter Second string : ");
        scanf("%s",str2);

        temp=strcmp(str1,str2);

        if(temp==0)
        {
                printf("\nBoth strings are equal");
        }
        else
        {
                printf("\nBoth string are not equal");
        }

        getch();

}



c program to compare two string with strcmp





Compare two strings with each other without using string handling function.

                                         When We compare two string without any string handling function we need to compare two string character by character.



#include<stdio.h>

void main()
{
        char str1[50],str2[50];
        int temp;
        clrscr();

        printf("Enter First string : ");
        scanf("%s",str1);

        printf("Enter Second string : ");
        scanf("%s",str2);

        temp=strcmp(str1,str2);

        if(temp==0)
        {
                printf("\nBoth strings are equal");
        }
        else
        {
                printf("\nBoth string are not equal");
        }

        getch();

}



c program to compare two string without strcmp

0 comments:

Post a Comment