A C Program for Comparing two string using user defined function, Without using string.h function : Wether they are equal or not

Leave a Comment
/* The following program is case sensitive, & let the user to enter 2 strings. After entering the 2 string the program check, character by character if any of the character is not equal it will generate the message of inequality otherwise they are equal. */

//CODE:
#include <stdio.h>
#include <stdlib.h>
int ostrcmp(char *,char *);

int main(void)
{
    char a[100], b[100];
    printf("NOTE: The program is case sensitive\n\n");
    printf("Enter the string 1:\n");
    gets(a);
    printf("Enter the string 2:\n");
    gets(b); 

        if (ostrcmp(a, b)) printf("Strings match.\n");
        else printf("Strings don't match.\n");
        system("pause");
        return 0;
}

int ostrcmp(char *str1, char *str2) {
        while (*str1 != '\0') {
                if (*str1 != *str2) return 0;
                str1++;
                str2++;
        }
        if (*str2 != '\0') return -1;
        return 1;
}


Screenshot:
A C Program for Comparing two string using user defined function, Without using string.h function : Wether they are equal or not
A C Program for Comparing two string using user defined function, Without using string.h function : Wether they are equal or not

0 comments: