C - strncmp() function
C strncmp() function - compare part of two strings
The strncmp() function is used to compare string1 and string2 to the maximum of n.
Use strncmp() function when:
- Partial Comparison is Needed: To verify if only a subset of characters match.
- Performance Optimization: When comparing only part of the strings saves time, such as when working with long strings.
- Prefix Matching: When you only need to compare prefixes, like identifying commands or keywords that start with certain characters.
Syntax:
int strncmp(const char *string1, const char *string2, size_t n)
Parameters:
Name | Description | Required /Optional |
---|---|---|
str1 | This is the first string to be compared. | Required |
str2 | This is the second string to be compared. | Required |
n | The maximum number of characters to be compared. | Required |
Return value from strncmp()
This function return values that are as follows −
Name | Value |
---|---|
string1 is less than string2 | < 0 |
string1 is identical to string2 | 0 |
string1 is greater than string2 | >0 |
Examples: strncmp() function
Example 1: Comparing only the first few characters of two strings
Following example demonstrates the strncmp() function by comparing only the first few characters of two strings. It uses the print_result function to output whether the strings are identical, less than, or greater than each other based on the specified comparison length.
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 10
int main(void)
{
int result;
int index = 3;
char string1[SIZE] = "pqruvxy";
char string2[SIZE] = "pqrxy";
void print_result( int, char *, char * );
result = strncmp( string1, string2, index);
printf( "\nComparison of only the first %i characters\n", index );
printf( " strncmp: " );
print_result( result, string1, string2);
index = 4;
result = strncmp( string1, string2, index);
printf( "\nComparison of only the first %i characters\n", index );
printf( " strncmp: " );
print_result( result, string1, string2 );
}
void print_result( int res, char * p_buffer1, char * p_buffer2 )
{
if ( res == 0 )
printf( "\"%s\" is identical to \"%s\"\n", p_buffer1, p_buffer2);
else if ( res < 0 )
printf( "\"%s\" is less than \"%s\"\n", p_buffer1, p_buffer2 );
else
printf( "\"%s\" is greater than \"%s\"\n", p_buffer1, p_buffer2 );
}
Output:
Comparison of only the first 3 characters strncmp: "pqruvxy" is identical to "pqrxy" Comparison of only the first 4 characters strncmp: "pqruvxy" is less than "pqrxy"
Example 2: Difference between strcmp() and strncmp()
This example shows the difference between strcmp() and strncmp() by first comparing two strings entirely with strcmp(), then comparing only the first few characters with strncmp(). This highlights how strncmp() can be useful for partial comparisons, while strcmp() checks the entire string.
#include <stdio.h>
#include <string.h>
#define SIZE 10
int main(void)
{
int result;
int index = 4;
char string1[SIZE] = "pqrswxyz";
char string2[SIZE] = "pqrstuvw";
void test( int, char *, char * );
result = strcmp( string1, string2 );
printf( "Comparison of each character\n" );
printf( "strcmp: " );
test( result, string1, string2 );
result = strncmp( string1, string2, index);
printf( "\nComparison of only the first %i characters\n", index );
printf( "strncmp: " );
test( result, string1, string2 );
}
void test( int res, char * p_string1, char * p_string2 )
{
if ( res == 0 )
printf( "\"%s\" is identical to \"%s\"\n", p_string1, p_string2);
else if ( res < 0 )
printf( "\"%s\" is less than \"%s\"\n", p_string1, p_string2 );
else
printf( "\"%s\" is greater than \"%s\"\n", p_string1, p_string2 );
}
Output:
Comparison of each character strcmp: "pqrswxyz" is greater than "pqrstuvw" Comparison of only the first 4 characters strncmp: "pqrswxyz" is identical to "pqrstuvw"
Example 3: Verifying command prefix
In this example, strncmp() is used to check if a user’s command input starts with "exit" (up to the first 4 characters).
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define the command and the user input
char command[] = "exit";
char user_input[] = "exit1234";
// Compare only the first 4 characters
if (strncmp(user_input, command, 4) == 0) {
printf("Command recognized as 'exit'.\n");
} else {
printf("Unknown command.\n");
}
return 0;
}
Output:
Command recognized as 'exit'.
Explanation:
- The program defines a command "exit" and a user input "exit1234".
- strncmp() checks if the first 4 characters of user_input match command.
- Since they do match, the program recognizes the input as the "exit" command.
Example 4: Case-Sensitive prefix comparison
This example compares two strings to see if they share the same case-sensitive prefix up to a specified number of characters.
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define two strings
char string1[] = "ApplePie";
char string2[] = "AppleJuice";
// Compare only the first 5 characters
if (strncmp(string1, string2, 5) == 0) {
printf("The strings share the same prefix.\n");
} else {
printf("The strings have different prefixes.\n");
}
return 0;
}
Output:
The strings share the same prefix.
Explanation:
- The program defines two strings, "ApplePie" and "AppleJuice".
- strncmp() compares the first 5 characters of both strings.
- Since the prefixes match, the program outputs that the strings share the same prefix.
C Programming Code Editor:
Previous C Programming: C strcmp()
Next C Programming: C strcoll()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming/string/c-strncmp.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics