C Program to Compare Two Strings Without Using strcmp() Last Updated : 05 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report String comparison refers to the process of comparing two strings to check if they are equal or determine their lexicographical order. C provides the strcmp() library function to compare two strings but in this article, we will learn how to compare two strings without using strcmp() function.The most straightforward method to compare two strings without using strcmp() function is by using a loop to compare the difference between ASCII values of each character in corresponding position. Let's take a look at example: C #include <stdio.h> #include <string.h> int comp(char *s1, char *s2) { // Compare characters one by one while (*s1 && !(*s1 - *s2)) { s1++; s2++; } // Returning 0 if strings are equal // +ve value if s1 is greater than s2 // -ve value if s1 is smaller than s2 return *s1 - *s2; } int main() { char s1[] = "Hello"; char s2[] = "Hello"; // Comparing s1 and s2 if(!comp(s1, s2)) printf("Equal"); else printf("Not Equal"); return 0; } OutputIdentical strings Explanation: This method iterates through both strings simultaneously, compares each character and returns the difference between the last compared characters. If the last compared character differs, it returns non-zero value. If they are same, we reached the null terminator '\0' in both strings, it returns 0.There is one more method using which we can compare two strings:Using Relational OperatorsThe two strings can also be compared by using relational operators without finding the difference between the ASCII values. C++ #include <stdio.h> int comp(char s1[], char s2[]) { int i = 0; // Compare characters one by one to determine // lexicographic order while (s1[i] != '\0' && s2[i] != '\0') { if (s1[i] != s2[i]) return (s1[i] > s2[i]) ? 1 : -1; i++; } // If one string ends before the other if (s1[i] == '\0' && s2[i] == '\0') { return 0; // Strings are equal } return (s1[i] == '\0') ? -1 : 1; } int main() { char s1[] = "Hello"; char s2[] = "Hello"; // Comparing s1 and s2 if (comp(s1, s2) == 0) printf("Equal"); else printf("Not Equal"); return 0; } OutputEqual Comment More infoAdvertise with us Next Article How to Compare Characters in C++? P pushkar_s Follow Improve Article Tags : C Language Similar Reads C program to copy string without using strcpy() function In C, the strcpy() function is commonly used for copying strings, but in this article, we will learn how to copy strings without using strcpy() function.The simplest method to copy string without using strcpy() function is by using a loop. Letâs take a look at an example:C#include <stdio.h> vo 2 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 5 min read C program to compare two files and report mismatches We are given almost two identical files. But some characters are corrupted. We need to find the line number and their position where those corrupted letter exists as well as Total number of corrupted (error) letters. Examples: Input : file1.txt contains it is fun file2.txt contains it is run Output 2 min read How to Compare Characters in C++? char in c is a keyword used for representing character data type. The memory size of char is 1 byte containing numbers, alphabets, and alphanumeric characters. We can compare the characters in C using 2 different ways: Comparison using ASCII values.Using the built-in function.1. Using ASCII Values A 3 min read C program to detect tokens in a C program As it is known that Lexical Analysis is the first phase of compiler also known as scanner. It converts the input program into a sequence of Tokens. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.For Example: 1) Keyword 5 min read Check if two strings are same or not Given two strings, the task is to check if these two strings are identical(same) or not. Consider case sensitivity.Examples:Input: s1 = "abc", s2 = "abc" Output: Yes Input: s1 = "", s2 = "" Output: Yes Input: s1 = "GeeksforGeeks", s2 = "Geeks" Output: No Approach - By Using (==) in C++/Python/C#, eq 7 min read Like