C Program to Compare Two Strings Using Pointers Last Updated : 28 Nov, 2024 Comments Improve Suggest changes 1 Likes Like Report In C, two strings are generally compared character by character in lexicographical order (alphabetical order). In this article, we will learn how to compare two strings using pointers.To compare two strings using pointers, increment the pointers to traverse through each character of the strings while comparing each character at corresponding position. If both strings are completely traversed without finding any mismatch, then they are considered equal. Otherwise, they are unequal. C #include <stdio.h> int strCmp(char *s1, char *s2) { // Traverse both strings until a mismatch // is found or one of the string ends while (*s1 && (*s1 == *s2)) { // Move to the next character in str1 s1++; s2++; } // If both are equal, this will return 0 return (*s1 - *s2); } int main() { char s1[] = "GeeksforGeeks"; char s2[] = "Geeks"; int res = strCmp(s1, s2); if (res < 0) { printf("\"%s\" is lexicographically smaller than \"%s\".\n", s1, s2); } else if (res > 0) { printf("\"%s\" is lexicographically greater than \"%s\".\n", s1, s2); } else { printf("\"%s\" is lexicographically equal to \"%s\".\n", s1, s2); } return 0; } Output"GeeksforGeeks" is lexicographically greater than "Geeks". Explanation: This method uses pointer dereferencing to compare characters in the strings using equality operator. The pointer is incremented to iterate through the strings until the end of one string is reached. Comment J jeshwanthb4mwy Follow 1 Improve J jeshwanthb4mwy Follow 1 Improve Article Tags : C Programs C Language C-Pointers C-String C Examples +1 More Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C5 min readPointers and StructuresPointers in C7 min readFunction Pointer in C6 min readUnions in C3 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like