C program to compare two strings

Write a C program to compare two strings using loop character by character. How to compare two strings without using inbuilt library function strcmp() in C programming. Comparing two strings lexicographically without using string library functions.

Write a C program to compare two strings using strcmp() library function. How to use strcmp() string library function. Comparing two different strings using inbuilt string library function in C programming.

Example:
Input string1: CodeforWin
Input string2: CodeforWin
Output: Both strings are equal

Required knowledge

Basic C programming, Loop, String, Functions

Program to compare two strings

/**
 * C program to compare two string without using string library functions
 */

#include <stdio.h>

#define MAX_SIZE 100 //Maximum size of the string


/* Function declarations */
int compare(char * string1, char * string2);



int main()
{
    char string1[MAX_SIZE], string2[MAX_SIZE];
    int res;


    /* Reads two strings from user */
    printf("Enter first string: ");
    gets(string1);
    printf("Enter second string: ");
    gets(string2);


    /* Calls function to compare both strings and stores result in res */
    res = compare(string1, string2);

    if(res == 0)
    {
        printf("Both strings are equal.\n");
    }
    else if(res == -1)
    {
        printf("First string is lexicographically smaller than second.\n");
    }
    else
    {
        printf("First string is lexicographically greater than second.\n");
    }

    return 0;
}




/**
 * Compares two strings lexicographically. 
 * Returns 0 if both strings are equal, 
 *        -1 if first string is smaller
 *         otherwise returns 1
 */
int compare(char * string1, char * string2)
{
    int i=0;

    /* Runs till both strings are equal */
    while(string1[i] == string2[i])
    {
        if(string1[i] == '\0' || string2[i] == '\0')
            break;

        i++;
    }

    if(string1[i-1] == '\0' && string2[i-1]=='\0')
        return 0;
    else if(string1[i] > string2[i])
        return 1;
    else if(string1[i] < string2[i])
        return -1;
}


Note: You can also use predefined library function strcmp(str1, str2) to compare two strings. This function returns -1 if first string is lexicographically smaller than second string, returns 0 if both string are lexicographically equal else returns 1 if first string is lexicographical greater than second string. This function is present under string.h header file.


Program to compare strings using strcmp()

/**
 * C program to compare two string using strcmp() function
 */

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

#define MAX_SIZE 100 //Maximum size of the string

int main()
{
    char string1[MAX_SIZE], string2[MAX_SIZE];
    int res;


    /* Reads two strings from user */
    printf("Enter first string: ");
    gets(string1);
    printf("Enter second string: ");
    gets(string2);


    /* Calls strcmp() to compare both strings and stores result in res */
    res = strcmp(string1, string2);

    if(res == 0)
    {
        printf("Both strings are equal.\n");
    }
    else if(res == -1)
    {
        printf("First string is lexicographically smaller than second.\n");
    }
    else
    {
        printf("First string is lexicographically greater than second.\n");
    }

    return 0;
} 


Output
Enter first string: CODEFORWIN
Enter second string: codeforwin
First string is lexicographically smaller than second.

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment