C String Operations


A string is an array of characters. C language offers predefined functions to perform string operations such as copy, compare, split, and more.

Copying strings using strcpy

strcpy (from ‘string copy’) copies a string. For example, this code snippet will copy the contents of the string variable second into the string variable first:

strcpy(first, second);

Here is an example of how manual implementation of the strcpy function looks like:

void copy_string(char first_string [], char second_string []) { int i; for(i = 0; second_string[i] != '\0'; i++) { first_string[i] = second_string[i]; } }

strncpy copies a specified number of characters from one string to a new string. For example, say we have a string second that we only want the first 5 characters of. We could copy it to a string first using:

strncpy(first, second, 5);

Note: both strcpy and strncpy make sure that the copied string ends in a null terminator, but this isn’t true for all string copying functions. strdup which allocates space for the new string and copies it doesn’t add a null terminator!

Concatenate strcat

strcat (from ‘string concatenate’) will concatenate a string, meaning it will take the contents of one string and place it on the end of another string. In this example, the contents of second will be concatenated onto first:

strcat(first, second);

Here is an example of manual implementation of function strcat:

void string_concatenate(char s1 [], char s2 []) { int i = strlen(s1), j; for(j = 0; s2[j]; j++, i += 1) { s1[i] = s2[j]; } }

Concatenate number of characters to a string: strncat

strncat (from ‘string number concatenate’) concatenates a certain number of characters from the beginning of the second string to the end of first string. In this example, strncat will concatenate some characters from the second to the first string:

strncat(char s1[], char s2[], int n);

Get length strlen

strlen (from ‘string length’) will return an integer value corresponding to the length of the string. Even though all strings are terminated with a \0 character, it will not be included in the count (so the length “hello” is indeed 5 and not 6). In this example, an integer called string_length will be assigned the length of my_string:

string_length = strlen(my_string);

Here is an manual implementation of fuction strlen:

int string_length(char string []) { int i; for(i = 0; string[i] != '\0'; i++); return i; }

Compare: strcmp

strcmp (from ‘string compare’) compares two strings. The integer value it returns is 0 if they are the same, but it will also return negative if the value of the first (by adding up characters) is less than the value of the second, and positive if the first is greater than the second. Take a look at an example of how this might be used:

if(!strcmp(first, second)){ printf("These strings are the same!\n"); } else { printf("These strings are not the same!\n"); }

Notice the !, which is needed because this function returns 0 if they are the same. Placing the exclamation point here will make that comparison return true.

Tip: If you found the ! strange, you can also compare the result of strcmp() with 0, like so –

if(strcmp(first, second) == 0){

We also have stricmp and strcmpi which compare two strings without case sensitivity. Similar to strcmp, stricmp/strcmpi will return 0 if the strings are equivalent, a negative value if first string is smaller, and a positive value if first string is greater.

Compare ‘n’ bytes: strncmp

strncmp compares two strings for ‘n’ characters. The integer value it returns is 0 if they are the same, but it will also return negative if the value of the first (by adding up characters) is less than the value of the second, and positive if the first is greater than the second. Take a look at an example of how this might be used:

if(!strncmp(first, second, 4)){ printf("These strings are the same!\n"); } else { printf("These strings are not the same!\n"); }

If the first string is wires and the second string is wired, the above snippet would still print These strings are the same! comparing first 4 characters.

Notice the !, which is needed because this function returns 0 if they are the same. Placing the exclamation point here will make that comparison return true.

Split a string: strtok

strtok (from ‘string token’) breaks a string into a series of tokens using a delimiter. In this example, strtok breaks string str into a series of tokens using the delimiter delim:

char *strtok(char *str, const char *delim);

Let’s see an example of how this being used:

void ParseInput(char [] input) { char *delim="|", *token; token=strtok(input,delim); printf("Code - %s\n",*token); token=strtok(NULL, delim); printf("Desc - %s\n",*token); }

Output of the above execution for input string US|United States would be,

Code - US Desc - United States

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.