How to play with strings in C


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Previous: Safety First Next: String Copy

Let's look at some of the operations that can be performed on a C string without modifying the string(s) involved.

Number of characters in a string - strlen

strlen returns the number of characters in a C string, excluding the NULL character.

Comparison of different strings - strcmp

strcmp is used to compare two different C strings. When the strings passed to strcmp contains exactly same characters in every index and have exactly same length, it returns 0. For example, i will be 0 in the following code:

char str1[] = "Look Here";
char str2[] = "Look Here";

int i = strcmp(str1, str2);

If the passed parameters aren't same, strcmp returns either a positive or a negative integer.

strcmp returns a negative integer if the first character that's mismatched between passed parameters has a lower ASCII value in the first string. For example:

char str1[] = "Look HerE";
char str2[] = "Look Here";

int i = strcmp(str1, str2);

i will be a negative number in the above example because the ASCII value of 'E' is lower than the ASCII value of 'e'. Refer to the ASCII table here. This is the first character (index 8) where these two strings differ.

A negative return value indicates that the first string would come before the second string if the strings are sorted in ascending order.

Try the following example, then modify strX arrays to your choice and see the result.

Comparison of different strings up to n-th character - strncmp

If you want to compare first n characters of two strings, then strncmp can be used. Its return value is similar to strcmp.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content