Comparing Strings
Definition: The strcmp function compares two strings character by character until a difference is found or the end of both strings is reached. The strncmp function works similarly, but compares at most a specified number of characters.
When you need to determine if two strings are the same or which one comes first in dictionary order, you use strcmp or strncmp. These functions compare strings lexicographically, which means they check each character in order, using the ASCII value for each character to decide which string is "less than," "equal to," or "greater than" the other. If all compared characters are the same and both strings end together, the strings are considered equal. If a difference is found, the comparison stops and the result is based on the first differing character.
main.c
1234567891011121314151617181920#include <stdio.h> #include <string.h> int main() { char str1[] = "apple"; char str2[] = "apricot"; int result = strcmp(str1, str2); if (result == 0) { printf("'%s' and '%s' are equal.\n", str1, str2); } else if (result < 0) { printf("'%s' comes before '%s'.\n", str1, str2); } else { printf("'%s' comes after '%s'.\n", str1, str2); } return 0; }
The return value of strcmp and strncmp tells you about the relationship between the two strings:
- If the return value is 0, the strings are equal;
- If the return value is less than 0, the first string is lexicographically less than the second string;
- If the return value is greater than 0, the first string is lexicographically greater than the second string. This means you can use the result directly in conditions to sort or check for equality.
main.c
1234567891011121314151617181920#include <stdio.h> #include <string.h> int main() { char str1[] = "banana"; char str2[] = "bandana"; int result = strncmp(str1, str2, 3); if (result == 0) { printf("The first 3 characters are equal.\n"); } else if (result < 0) { printf("The first 3 characters of '%s' come before those of '%s'.\n", str1, str2); } else { printf("The first 3 characters of '%s' come after those of '%s'.\n", str1, str2); } return 0; }
You should use strncmp instead of strcmp when you only want to compare a certain number of characters, not the entire string. This is useful if you want to check if two strings share a common prefix, or if you are working with data where only a portion of the string is relevant for comparison. Always make sure the number of characters you compare does not exceed the actual length of either string to avoid unexpected results.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Чудово!
Completion показник покращився до 5.26
Comparing Strings
Свайпніть щоб показати меню
Definition: The strcmp function compares two strings character by character until a difference is found or the end of both strings is reached. The strncmp function works similarly, but compares at most a specified number of characters.
When you need to determine if two strings are the same or which one comes first in dictionary order, you use strcmp or strncmp. These functions compare strings lexicographically, which means they check each character in order, using the ASCII value for each character to decide which string is "less than," "equal to," or "greater than" the other. If all compared characters are the same and both strings end together, the strings are considered equal. If a difference is found, the comparison stops and the result is based on the first differing character.
main.c
1234567891011121314151617181920#include <stdio.h> #include <string.h> int main() { char str1[] = "apple"; char str2[] = "apricot"; int result = strcmp(str1, str2); if (result == 0) { printf("'%s' and '%s' are equal.\n", str1, str2); } else if (result < 0) { printf("'%s' comes before '%s'.\n", str1, str2); } else { printf("'%s' comes after '%s'.\n", str1, str2); } return 0; }
The return value of strcmp and strncmp tells you about the relationship between the two strings:
- If the return value is 0, the strings are equal;
- If the return value is less than 0, the first string is lexicographically less than the second string;
- If the return value is greater than 0, the first string is lexicographically greater than the second string. This means you can use the result directly in conditions to sort or check for equality.
main.c
1234567891011121314151617181920#include <stdio.h> #include <string.h> int main() { char str1[] = "banana"; char str2[] = "bandana"; int result = strncmp(str1, str2, 3); if (result == 0) { printf("The first 3 characters are equal.\n"); } else if (result < 0) { printf("The first 3 characters of '%s' come before those of '%s'.\n", str1, str2); } else { printf("The first 3 characters of '%s' come after those of '%s'.\n", str1, str2); } return 0; }
You should use strncmp instead of strcmp when you only want to compare a certain number of characters, not the entire string. This is useful if you want to check if two strings share a common prefix, or if you are working with data where only a portion of the string is relevant for comparison. Always make sure the number of characters you compare does not exceed the actual length of either string to avoid unexpected results.
Дякуємо за ваш відгук!