🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Strings are a fundamental concept in programming, and often, there's a need to compare them. While there are built-in functions to do this, understanding the process at a lower level using pointers offers valuable insight. In this tutorial, we will look at how to compare two strings manually using pointers.
2. Program Overview
1. Declare two character arrays (strings).
2. Declare two character pointers.
3. Use the pointers to traverse and compare the strings character by character.
4. Return the comparison result.
3. Code Program
#include <stdio.h>
// Function to compare two strings using pointers
int compareStrings(char *str1, char *str2) {
while (*str1 && (*str1 == *str2)) {
str1++;
str2++;
}
return *(unsigned char *)str1 - *(unsigned char *)str2;
}
int main() {
char string1[100], string2[100];
// Input the strings
printf("Enter the first string:\n");
gets(string1);
printf("Enter the second string:\n");
gets(string2);
// Compare the strings
int result = compareStrings(string1, string2);
// Display the comparison result
if(result == 0) {
printf("Both strings are identical.\n");
} else if(result > 0) {
printf("The first string is greater than the second.\n");
} else {
printf("The second string is greater than the first.\n");
}
return 0;
}
Output:
Enter the first string: Hello Enter the second string: Hello Both strings are identical.
4. Step By Step Explanation
1. We begin with the declaration of two character arrays, string1, and string2, to store our input strings.
2. The compareStrings function is designed to compare two strings. This function uses two pointers (str1 and str2) to iterate through the characters of each string. The while loop inside this function will continue as long as characters from both strings are equal and not the null terminator.
- If a difference is found or one of the strings terminates before the other, the loop will stop, and the function will return the difference of the ASCII values of the characters where the mismatch is first found.
3. In the main function, the user is prompted to input two strings. We use the gets function to read them (note that gets can be risky due to potential buffer overflows; it's used here for simplicity).
4. The compareStrings function is called to compare the two strings.
5. Depending on the returned result:
- If it's 0, the strings are identical.
- If it's positive, the first string is lexicographically greater.
- If it's negative, the second string is lexicographically greater.
6. The comparison result is then displayed to the user.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment