🎓 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
The strcmp() function in C is a standard library function that compares two strings. It is part of the C standard library (string.h). This function is useful for comparing the lexicographical order of two strings.
Table of Contents
- Introduction
strcmp()Function Syntax- Understanding
strcmp()Function - Examples
- Comparing Two Identical Strings
- Comparing Different Strings
- Real-World Use Case
- Conclusion
Introduction
The strcmp() function compares two null-terminated strings lexicographically. It returns an integer less than, equal to, or greater than zero if the first string is found to be less than, equal to, or greater than the second string, respectively.
strcmp() Function Syntax
The syntax for the strcmp() function is as follows:
int strcmp(const char *str1, const char *str2);
Parameters:
str1: A pointer to the first null-terminated string to be compared.str2: A pointer to the second null-terminated string to be compared.
Returns:
- The function returns an integer:
- Less than zero if
str1is less thanstr2. - Zero if
str1is equal tostr2. - Greater than zero if
str1is greater thanstr2.
- Less than zero if
Understanding strcmp() Function
The strcmp() function performs a lexicographical comparison of the two strings, which means it compares the strings character by character using the ASCII values of the characters. The comparison stops when a difference is found or the end of the strings is reached.
Examples
Comparing Two Identical Strings
To demonstrate how to use strcmp() to compare two identical strings, we will write a simple program.
Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, World!";
char str2[] = "Hello, World!";
// Compare the strings using strcmp
int result = strcmp(str1, str2);
// Print the result
printf("Comparison result: %d\n", result);
return 0;
}
Output:
Comparison result: 0
Comparing Different Strings
This example shows how to use strcmp() to compare different strings.
Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, World!";
char str2[] = "Hello, Ramesh!";
// Compare the strings using strcmp
int result = strcmp(str1, str2);
// Print the result
if (result < 0) {
printf("\"%s\" is less than \"%s\"\n", str1, str2);
} else if (result > 0) {
printf("\"%s\" is greater than \"%s\"\n", str1, str2);
} else {
printf("\"%s\" is equal to \"%s\"\n", str1, str2);
}
return 0;
}
Output:
"Hello, World!" is less than "Hello, Ramesh!"
Real-World Use Case
Sorting an Array of Strings
In real-world applications, the strcmp() function can be used to sort an array of strings lexicographically.
Example: Sorting an Array of Strings
#include <stdio.h>
#include <string.h>
void sortStrings(char arr[][20], int n) {
char temp[20];
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(arr[i], arr[j]) > 0) {
strcpy(temp, arr[i]);
strcpy(arr[i], arr[j]);
strcpy(arr[j], temp);
}
}
}
}
int main() {
char arr[][20] = {"Ramesh", "Suresh", "Mahesh", "Naresh", "Kalpesh"};
int n = sizeof(arr) / sizeof(arr[0]);
// Sort the array of strings
sortStrings(arr, n);
// Print the sorted array
printf("Sorted array of strings:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
Output:
Sorted array of strings:
Kalpesh
Mahesh
Naresh
Ramesh
Suresh
Conclusion
The strcmp() function is used for comparing strings in C. By understanding and using this function correctly, you can efficiently compare and sort strings in your programs. Always ensure that the strings are null-terminated to prevent undefined behavior. This function is particularly useful for tasks that involve sorting, searching, and lexicographical comparisons of strings.
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