C Program to Compare Two Strings Without Using the strcmp() Function

In this article, we will write a C program to compare two strings without using the strcmp() function. The program takes the two strings from the user as input, compares both, and prints the result on the output window.

Sample Example:

Enter the first string: Hii there
Enter the second string: Hii there
Both strings are indentical

Enter the first string: Hello
Enter the second string: hel
Strings are not the same

In C programming, a string is a sequence of characters terminated by a null character('\0'), which indicates the end of the string.

To compare two strings, we can use the following algorithm:

  • Run a while loop over both strings until the end of any of the two strings is reached.
  • Compare characters that are at the same index in both strings.
  • If all characters that are at the same index are equal, it means that both strings are identical
  • If not, it means that the strings are not the same.

As the last character of a string in C is always a null character('\0'), we can use it to check if the end of the string is reached or not. This will help us terminate the while loop as soon as the end of any of the two strings is reached.

The following C program shows how you can compare two strings without using the strcmp() function:

// C program to compare two strings without
// using the strcmp() function

#include <stdio.h>
#include <conio.h> 

int main() {
	
	char str1[100], str2[100];
	int i = 0;
	
	printf("Enter the first string: ");
	gets(str1);
	
    printf("Enter the second string: ");
	gets(str2);
	
	// Run the while loop until end of the strings is reached
	while(str1[i]!='\0' && str2[i]!='\0'){
		
		// Check if characters at index i in 
		// both strings are not equal
		if(str1[i] != str2[i]){
			break;
		}
		i++;
	}
	
	if(str1[i]=='\0' && str2[i]=='\0'){
		printf("Both strings are indentical");
	}
	else{
		printf("Strings are not the same");
	}
	
	return 0;

}

Output:

Enter the first string: ProgrammersPortal
Enter the second string: ProgrammersPortal
Both strings are indentical

In the above program, we are basically running a while loop over both strings and then comparing the characters that are at the same index.

If all characters that are at the same index are equal, we will reach the end of both strings when the loop finishes. This means, the variable i will be equal to the index of the null character if both strings are equal.

Therefore, we are checking if the character at the ith index in both strings is the same as the null character or not. If yes, both strings are equal, otherwise not.


Using Pointers to Compare Two Strings

We can also use pointers to compare strings in C. A pointer in C programming is a variable that holds the memory address of another variable.

In the following C program, we have used pointers to compare two strings:

// C program to compare two strings using pointers

#include <stdio.h>
#include <conio.h> 

int main() {
	
	char str1[100], str2[100];
	char *a, *b;
	
	printf("Enter the first string: ");
	gets(str1);
	
    printf("Enter the second string: ");
	gets(str2);
	
	// Hold the memory address of the first character of both strings
	a = str1;
	b = str2;
	
	// Run the while loop until end of the strings is reached
	while(*a!='\0' && *b!='\0'){
		
		// Check if current character in string 1
		// is not equal to the current character in string 2
		if(*a != *b){
			break;
		}
		
		// Move to next characters in both strings
		a++;
		b++;
	}
	
	// If both pointers points to EOS, strings are equal 
	if(*a=='\0' && *b=='\0'){
		printf("Both strings are indentical");
	}
	else{
		printf("Strings are not the same");
	}
	
	return 0;

}

Output:

Enter the first string: Hii there
Enter the second string: Hello
Strings are not the same

Program Explanation:

Here is a detailed explanation of the above program:

  • We have created two pointer variables a and b to hold the memory address of str1 and str2 respectively.
  • The pointers a and b are assigned the memory addresses of the first characters of str1 and str2, respectively.
  • A while loop is used to compare the characters of the two strings using pointers (a and b).
  • The loop runs until the end of either string is reached
  • If the characters pointed to by a and b are not equal, the loop is terminated.
  • After the loop, if both pointers point to the end-of-string ('\0') character, it means the strings are identical.
  • If the loop is terminated before reaching the end of both strings, the strings are not the same.

That’s how it works.

I hope you will find this article helpful. Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts

Leave a Comment