DEV Community

loading...

String Functions in C

Tomislav Kraljic
Hey there! I am a Software Engineer who is currently on the path to transition into DevOps.
Updated on ・5 min read

In this article, I will cover all the main string functions you can use in C.


strlen()

  • To get the total length of a string, you can use the strlen() function.
  • To use this function, just pass in the string you want as an argument.

Alt Text

  • Here, we create a string array and set a name to it.
  • Then, inside the printf() function, we pass in strlen() with name of the variable as an argument.
  • The return type of strlen() is of size_t. Since that is the return type, our format specifier will be "%zu".

strcpy()

  • Since, you can't define and declare arrays separately in C, you can't define and declare strings separately either.
  • You can use strcpy() to copy a string to an existing string.
  • strcpy() takes in two arguments: the source string and destination.

Alt Text

  • Here, we create a string array without a value.
  • Then, we copy a magic string to the string array.
  • Finally, we print it out on the terminal

strncpy()

  • strncpy() is very similar to strcpy(). However, this function takes in a 3rd argument. Third argument is the max number of characters to copy.
  • The problem with strcpy() is that it does not check if the source string can actually fit into the destination target. This can cause buffer overflow, loss of data, and can lead to many security vulnerabilities in your program.
  • It is always preferred to use strncpy() over strcpy().

Alt Text

  • Here, we declare a string array of first_name that has a size of 10 characters.
  • Then, we use strncpy() to copy "Tomislav" into the string array. However, we also set the size to the total size allotted for the string array. This will prevent any buffer overflow from occuring.

strcat()

  • strcat() takes two strings and concatenates them together.
  • strcat() takes two arguments: the first string and the second string.
  • The concatenated string becomes a new string.

Alt Text

  • Here we are asking the user for two inputs.
  • After the user give two inputs, both are concatenated or combined into one string.

strcmp()

  • strcmp() compares two strings against each other.
  • This function can not compare arrays or characters.
  • This function compares the ASCII integer value of the character one by one.
  • If the return is less than 0, that means string 1 is less than string 2.
  • If the return is 0, it means the strings are the same.
  • If the return is greater than 0, it means that string 2 is less than string 1.
  • It is important to note that uppercase letters will have a smaller ASCII value than lowercase letters.

Alt Text


strncmp()

  • strncmp() compares two strings until they differ or until it has a compared number of characters specified in the 3rd argument.
  • strncmp() is generally used for prefixes while strcmp() is used for entire strings.

Alt Text


strchr()

  • strchr() searches a given string for a specified character.
  • This function takes in two arguments. The 1st argument is the string that is being searched. The 2nd argument is the character you are looking for.

Alt Text

  • Here, we create a string array of "Tomislav". Then, we create a char variable with the value of "T". Finally, we create a pointer variable and set it to the strchr() function where we pass the string array and character we are comparing as arguments.
  • Then, we create a conditional. If we can't find it, print "No results are found...", else, print this statement.

strstr()

  • strstr() searches one string for the occurrence of a substring.
  • It returns a pointer to the position in the string where the substring was found.
  • If no match is found, it returns NULL.

Alt Text

  • Here, we are doing something very similar with the strchr() function. However, instead of using a character, we are using another string that is acting as a substring.

strtok()

  • strtok() is used for tokenizing a string.
  • A token is a sequence of characters within a string that is bound by a delimiter.
  • A delimiter is something like a comma, period, or space.
  • Breaking a sentence into words is referred to as "tokenizing"
  • strtok() takes in 2 arguments. The 1st argument is the string that is being tokenized. The 2nd argument is all the possible delimiters.

Alt Text

  • Here, we create a string with delimiters.
  • We set the delimiter to a variable.
  • Then, we call strtok() and print it to the console.
  • The end result is all the delimiters from the token are removed.

toupper()

  • toupper() converts from lowercase to uppercase.
  • This can be used with characters and strings.
  • To use it in strings, you must loop over the string, call the toupper() function when you are printing it.

Alt Text

tolower()

  • tolower() converts from uppercase to lowercasecase.
  • This can be used with characters and strings.
  • To use it in strings, you must loop over the string, call the tolower() function when you are printing it.

Alt Text


atof()

  • This converts a string to a float.
  • There are many different variations with this.
  • atoi() converts a string to an integer.
  • atol() converts a string to a long.
  • atoll() converts a string to a long long.

Alt Text


These are most of the string functions that you will use. There are definitely some more. If you are interested, you can check them out here: https://www.tutorialspoint.com/c_standard_library/string_h.htm

I hope these have helped you and cleared any confusion you may have had along the way.

Feel free to comment below if you have any questions!

Discussion (0)