The Wayback Machine - https://web.archive.org/web/20241127003407/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
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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)



Previous Article
Next Article

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 < 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 & 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->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 "@# >" 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
Program to compare two fractions
Given two fractions a/b and c/d, compare them and print the larger of the two. Examples : Input: 5/6, 11/45Output: 5/6 Input: 4/5 and 2/3Output: 4/5 We can simply convert the fractions into floating-point values by dividing the numerator by the denominator. Once we have the two floating-point numbers corresponding to each fraction, we can compare t
5 min read
Compare Strings for equality or lexicographical order in different programming Languages
Given two strings string1 and string2, the task is to check if these two strings are equal or not. Examples: Input: string1 = “GeeksforGeeks”, string2 = “GeeksforGeeks” Output: Yes Input: string1 = “Geeks for Geeks”, string2 = “Geeks for Geeks” Output: Yes Input: string1 = “GeeksforGeeks”, string2 = “Geeks” Output: No Input: string1 = “Geeks for Ge
8 min read
How to Initialize and Compare Strings in Java?
String is one of the most basic data types and it is widely used in storing and manipulating pieces of text. One of the fundamental features of strings in Java is their immutability that is once a string is created, it will never be changed. Immutability creates two general ways of initialization with strings in Java using the String Pool. This art
5 min read
Check if two strings are same or not without using library functions
Given two strings S1 and S2, the task is to check whether they are the same or not without using string library functions. Examples: Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksForGeeks”Output: TrueExplanation:S1 and S2 are the same strings Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksforGeeks”Output: False Approach: Follow the steps below to solve the prob
5 min read
Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | Set 2
Given two equal-size strings s[] and t[] of size N. In one step, choose any character of t[] and replace it with another character. Return the minimum number of steps to make t[] an anagram of s[]. Note: An Anagram of a string is a string that contains the same characters with a different (or the same) ordering. Examples: Input: s = "baa", t = "aba
6 min read
Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
Given two strings s1 and s2, we need to find the minimum number of manipulations required to make two strings anagram without deleting any character. Note:- The anagram strings have same set of characters, sequence of characters can be different. If deletion of character is allowed and cost is given, refer to Minimum Cost To Make Two Strings Identi
10 min read
Compare two floating-point numbers given in Scientific Notations
Given two strings N and M in the form of a * 10 b. The task is to compare given two floating-point numbers and print the smaller number and if both the numbers are equal then print Equal. 0<|a|<10^9 and -10^9<b<10^9. Example: N and M are two numbers with two parts: a1 is mantissa of N and a2 is mantissa of M.b1 is exponent of N and b2 i
11 min read
Compare given two powers of 10
Given 4 integers A, B, Z1, and Z2. The task is to compare A*10Z1 and B*10Z2. Examples: Input: A = 19, Z1 = 2, B = 20, Z2 = 1Output: A > BExplanation:A can be written as 1900 B can be written as 200 So, A is greater than B. Input:, A = 199, Z1 =10, B = 96, Z2 = 1000Output: A < BExplanation:A can be written as 19900000.... B can be written as 9
7 min read
Compare Two Arrays in Java
While comparing two arrays we can not use "==" operator as it will compare the addresses of the memory block to which both the arrays are pointing. If we want to compare the elements inside the array we need to figure out other ways instead of using arithmetic operators. As we all know arrays data structures possess the property of containing eleme
6 min read
Compare two Version numbers
A version number is a string that is used to identify the unique state of a software product. A version number looks like a.b.c.d, where a, b, etc are numbers, so the version number is a string in which numbers are separated by dots. These numbers generally represent the hierarchy from major to minor (a is major and d is minor). In this problem, we
15+ min read
C/C++ Program to Count number of binary strings without consecutive 1's
Write a C/C++ program for a given positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1s. Examples: Input: N = 2Output: 3// The 3 strings are 00, 01, 10 Input: N = 3Output: 5// The 5 strings are 000, 001, 010, 100, 101 Recommended: Please solve it on “PRACTICE ” first, before
4 min read
Program to compare m^n and n^m
Given two positive integers m and n, the task is to write a program that checks whether m^n is greater than, less than, or equal to n^m. Examples : Input: m = 3, n = 10 Output: m^n > n^m Explanation : 3^10=59049 which is greater than 10^3=1000 Input: m = 987654321, n = 123456987 Output: m^n < n^m A naive approach is to compute m^n and n^m, wh
4 min read
Number of common base strings for two strings
Given two strings s1 and s2, we need to find number of common base strings of two. A substring of a string s is called base string if repeated concatenation of the substring results in s. Examples: Input : s1 = "pqrspqrs" s2 = "pqrspqrspqrspqrs" Output : 2 The two common base strings are "pqrs" and "pqrspqrs". Input: s1 = "bbb" s2 = "bb" Output: 1
6 min read
Count of same length Strings that exists lexicographically in between two given Strings
Given two string S1 and S2 of length L, the task is to count the number of strings of length L, that exists in between S1 and S2, which are lexicographically greater than S1 but smaller than S2. Examples: Input: S1 = "b", S2 = "f" Output: 3 Explanation: These are 3 strings which come lexicographically in between S1 and S2 i.e. "c", "d" & "e"Inp
8 min read
Check whether two strings can be made equal by reversing substring of equal length from both strings
Give two strings S1 and S2, the task is to check whether string S1 can be made equal to string S2 by reversing substring from both strings of equal length. Note: A substring can be reversed any number of times. Example: Input: S1 = "abbca", S2 = "acabb" Output: Yes Explanation: The string S1 and S2 can be made equal by: Reverse S1 in the range [2,
12 min read
Check if given strings can be made same by swapping two characters of same or different strings
Given an array of equal-length strings, arr[] of size N, the task is to check if all the strings can be made equal by repeatedly swapping any pair of characters of same or different strings from the given array. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: arr[] = { "acbdd", "abcee" } Output: YES Explanation: Swapp
11 min read
Check if two strings can be made equal by reversing a substring of one of the strings
Given two strings X and Y of length N, the task is to check if both the strings can be made equal by reversing any substring of X exactly once. If it is possible, then print "Yes". Otherwise, print "No". Examples: Input: X = "adcbef", Y = "abcdef"Output: YesExplanation: Strings can be made equal by reversing the substring "dcb" of string X. Input:
12 min read
Pairs of complete strings in two sets of strings
Two strings are said to be complete if on concatenation, they contain all the 26 English alphabets. For example, "abcdefghi" and "jklmnopqrstuvwxyz" are complete as they together have all characters from 'a' to 'z'. We are given two sets of sizes n and m respectively and we need to find the number of pairs that are complete on concatenating each st
15 min read
Meta Strings (Check if two strings can become same after a swap in one string)
Given two strings, the task is to check whether these strings are meta strings or not. Meta strings are the strings which can be made equal by exactly one swap in any of the strings. Equal string are not considered here as Meta strings. Examples: Input : str1 = "geeks" str2 = "keegs"Output : YesBy just swapping 'k' and 'g' in any of string, both wi
8 min read
Javascript program to swap two numbers without using temporary variable
To swap two numbers without using a temporary variable, we have multiple approaches. In this article, we are going to learn how to swap two numbers without using a temporary variable. Below are the approaches used to swap two numbers without using a temporary variable: Table of Content Using Arithmetic OperatorsUsing Bitwise XORA mixture of bitwise
6 min read
three90RightbarBannerImg