🎓 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 strncmp() function in C is a standard library function that compares up to a specified number of characters from two strings. It is part of the C standard library (string.h). This function is useful for comparing the beginning parts of two strings, ensuring a maximum number of characters are compared.
Table of Contents
- Introduction
strncmp()Function Syntax- Understanding
strncmp()Function - Examples
- Comparing the Beginning of Two Identical Strings
- Comparing Different Strings
- Real-World Use Case
- Conclusion
Introduction
The strncmp() function compares up to n characters from 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, based on the first n characters.
strncmp() Function Syntax
The syntax for the strncmp() function is as follows:
int strncmp(const char *str1, const char *str2, size_t n);
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.n: The maximum number of characters to compare.
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 strncmp() Function
The strncmp() function performs a lexicographical comparison of up to n characters 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, n characters are compared, or the end of one of the strings is reached.
Examples
Comparing the Beginning of Two Identical Strings
To demonstrate how to use strncmp() to compare the beginning of 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 first 5 characters of the strings using strncmp
int result = strncmp(str1, str2, 5);
// Print the result
printf("Comparison result: %d\n", result);
return 0;
}
Output:
Comparison result: 0
Comparing Different Strings
This example shows how to use strncmp() to compare different strings.
Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello, World!";
char str2[] = "Hello, Ramesh!";
// Compare the first 7 characters of the strings using strncmp
int result = strncmp(str1, str2, 7);
// Print the result
if (result < 0) {
printf("\"%s\" is less than \"%s\" (first 7 characters)\n", str1, str2);
} else if (result > 0) {
printf("\"%s\" is greater than \"%s\" (first 7 characters)\n", str1, str2);
} else {
printf("\"%s\" is equal to \"%s\" (first 7 characters)\n", str1, str2);
}
return 0;
}
Output:
"Hello, World!" is greater than "Hello, Ramesh!" (first 7 characters)
Real-World Use Case
Comparing Version Numbers
In real-world applications, the strncmp() function can be used to compare version numbers or other prefix-based comparisons.
Example: Comparing Version Numbers
#include <stdio.h>
#include <string.h>
int main() {
char version1[] = "1.2.3";
char version2[] = "1.2.4";
// Compare the first 3 characters of the version numbers using strncmp
int result = strncmp(version1, version2, 3);
// Print the result
if (result < 0) {
printf("Version %s is less than version %s (first 3 characters)\n", version1, version2);
} else if (result > 0) {
printf("Version %s is greater than version %s (first 3 characters)\n", version1, version2);
} else {
printf("Version %s is equal to version %s (first 3 characters)\n", version1, version2);
}
return 0;
}
Output:
Version 1.2.3 is equal to version 1.2.4 (first 3 characters)
Conclusion
The strncmp() function is used for comparing the first n characters of two strings in C. By understanding and using this function correctly, you can efficiently perform partial string comparisons in your programs. This is particularly helpful in applications that involve prefix-based comparisons, such as version numbers or partially matching strings. Always ensure that the strings are null-terminated to prevent undefined behavior.
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