The Wayback Machine - https://web.archive.org/web/20240721030504/https://www.geeksforgeeks.org/c-program-to-compare-two-strings-without-using-strcmp-function/
Open In App

C program to Compare Two Strings without using strcmp() function

Last Updated : 06 Sep, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Given two strings s1 and s2, the task is to write C program compare the two strings without using strcmp() function. If string are equal then print “Equal strings” else print “Unequal strings”.

Examples:

Input: s1 = “geeksforgeeks”, s2 = “geeks”
Output: Unequal Strings

Input: s1 = “geeksforgeeks”, s2 = “geeksforgeeks”
Output: Equal Strings

 

Approach: There are three possible cases occur when we compare two strings:

  1. Both the strings are the same means difference of ASCII value between both the strings is 0.
  2. Both the strings are different means ASCII value of first not matching character in the first string is less than the second string then the difference between both the strings is (<0).
  3. Both the strings are different means ASCII value of first not matching character in the first string is greater than the second string then the difference between both the strings is (>0).

Based on the above three conditions, the idea is to compare each character of the given strings one by one whenever condition 2 or 3 occurs then print “Unequal strings” else print “Equal strings”.

Below is the implementation of the above approach:

C




// C program to compare the two strings
// without using strcmp() function
#include <stdio.h>
 
// Function that compares the two string
void compareStrings(char* x, char* y)
{
    int flag = 0;
 
    // Iterate a loop till the end
    // of both the strings
    while (*x != '\0' || *y != '\0') {
        if (*x == *y) {
            x++;
            y++;
        }
 
        // If two characters are not same
        // print the difference and exit
        else if ((*x == '\0' && *y != '\0')
                 || (*x != '\0' && *y == '\0')
                 || *x != *y) {
            flag = 1;
            printf("Unequal Strings\n");
            break;
        }
    }
 
    // If two strings are exactly same
    if (flag == 0) {
        printf("Equal Strings\n");
    }
}
 
// Driver Code
int main(void)
{
    // Given strings s1 and s2
    char s1[20] = "python";
    char s2[20] = "dsa";
 
    // Function Call
    compareStrings(s1, s2);
    return 0;
}


Output

Unequal Strings

Time Complexity: O(N)
Auxiliary Space: O(1)



Similar Reads

C++ program to compare two Strings using Operator Overloading
Pre-requisite: Operator Overloading in C++Given two strings, how to check if the two strings are equal or not, using Operator Overloading. Examples: Input: ABCD, XYZ Output: ABCD is not equal to XYZ ABCD is greater than XYZ Input: Geeks, Geeks Output: Geeks is equal to Geeks Approach: Using binary operator overloading. Declare a class with a string
3 min read
C Program to Compare Two Strings Using Pointers
In C, strings are arrays of characters terminated by a null character ('\0') and pointers can be used to traverse these arrays and compare the characters in two strings. In this article, we will learn how to compare two strings using pointers in C. For Example, Input:char str1[] = "Hello";char str2[] = "World";Output:Hello and World are not EqualCo
2 min read
Write your own strcmp that ignores cases
Write a modified strcmp function which ignores cases and returns -1 if s1 &lt; s2, 0 if s1 = s2, else returns 1. For example, your strcmp should consider "GeeksforGeeks" and "geeksforgeeks" as same string.Source: Microsoft Interview Set 5Following solution assumes that characters are represented using ASCII representation, i.e., codes for 'a', 'b',
6 min read
Compare two integers without using any Comparison operator
Given two integers A &amp; B. Task is to check if A and B are same or not without using comparison operators.Examples: Input : A = 5 , B = 6Output : 0 Input : A = 5 , B = 5 Output : 1 Note : 1 = "YES" and 0 = "NO" The idea is pretty simple we do Xor of both elements ( A, B ). if Xor is zero then two numbers are equal else not. Below is the implemen
5 min read
C Program to Compare Two Strings Lexicographically
Here, we will build a C Program to compare two strings lexicographically. Given 2 strings s1 and s2, of lowercase English alphabets, denote the lexicographically greater string. Lexicographical order is the order in which words appear in the order of a dictionary. Input: s1 = "geeks", s2 = "geeksforgeeks Output: String 2 is lexicographically greate
2 min read
Compare two strings represented as linked lists
Given two strings, represented as linked lists (every character is a node in a linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are the same, 1 if the first linked list is lexicographically greater, and -1 if the second string is lexicographically greater. Examples: Input: list1 = g-&gt;e-
7 min read
Compare two strings considering only alphanumeric characters
Given two strings that can contain lower and uppercase alphabets, numbers and special characters like dots, blank spaces, commas, etc. Compare both strings considering only alphanumeric characters([a-b], [A-B] and [0-9]) if they are equal or not. For example, strings "Ram, Shyam" and "Ram-Shyam" both are the same and also "/.';[]" and "@# &gt;" are
10 min read
Compare two Linked List of Strings
Given two linked lists L1 and L2 in which in every node a string is stored. The task is to check whether the strings combining all the nodes are similar or not. Examples: Input: L1 = ["He", "llo", "wor", "ld"], L2 = ["H", "e", "ll", "owo", "r", "ld"]Output: trueExplanation: both lists makes the string of "Helloworld". Input: L1 = ["w", "o", "l", "d
11 min read
Compare two strings lexicographically in Java
In this article, we will discuss how we can compare two strings lexicographically in Java. One solution is to use Java compareTo() method. The method compareTo() is used for comparing two strings lexicographically in Java. Each character of both the strings is converted into a Unicode value for comparison. int compareTo(String str) :It returns the
3 min read
C Program to Concatenate Two Strings Without Using strcat
In C, concatenating two strings means combining two strings to form one string. In this article, we will learn how to concatenate two strings without using the strcat() function in C. For Example, Input: str1= "Hello,"str2= "Geek!"Output:Concatenated String: Hello, Geek!Concatenating Two Strings in CTo concatenate two strings without using the strc
2 min read
three90RightbarBannerImg