Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Comparing Strings | String Manipulation Functions
Working with Strings in C

bookComparing Strings

Note
Definition

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

main.c

copy
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

main.c

copy
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.

question mark

What does a return value of 0 from strcmp indicate?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you give an example of how to use `strcmp` and `strncmp` in code?

What happens if the strings have different lengths?

When should I use `strcmp` versus `strncmp`?

bookComparing Strings

Pyyhkäise näyttääksesi valikon

Note
Definition

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

main.c

copy
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

main.c

copy
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.

question mark

What does a return value of 0 from strcmp indicate?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 3
some-alt