String Functions in C
Tags: computerscience, c, programming
Published: 2021-04-29 07:44:23

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

<hr/>

*strlen()*
<ul> 
    <li>To get the total length of a string, you can use the strlen() function. </li>
    <li> To use this function, just pass in the string you want as an argument. </li>
</ul>


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cr5mm4jf7id4p0j5zboh.png)

<ul>
    <li>Here, we create a string array and set a name to it.</li>

    <li>Then, inside the printf() function, we pass in strlen() with name of the variable as an argument. </li>

    <li>The return type of strlen() is of size_t. Since that is the return type, our format specifier will be "%zu". </li>
</ul>

<hr/>

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmmm0uc2e77nrfrrsdmu.png)

<ul>
    <li> Here, we create a string array without a value. </li>
    <li> Then, we copy a magic string to the string array. </li>
    <li> Finally, we print it out on the terminal </li>
</ul>

<hr/>

strncpy()
<ul>
    <li> strncpy() is very similar to strcpy(). However, this function takes in a 3rd argument. Third argument is the max number of characters to copy. </li>
    <li> 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. </li>
    <li> It is always preferred to use strncpy() over strcpy().</li>
</ul>




![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o40k1sx61zxfvapurkm8.png)

<ul>
   <li> Here, we declare a string array of first_name that has a size of 10 characters. </li>
   <li> 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. </li>
</ul>



</hr>

*strcat()*
<ul>
    <li>strcat() takes two strings and concatenates them together. </li>
    <li>strcat() takes two arguments: the first string and the second string. </li>
    <li> The concatenated string becomes a new string. </li>
</ul>


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13ij5p4j8isdv3j0iigt.png)

<ul>
    <li> Here we are asking the user for two inputs. </li>
    <li> After the user give two inputs, both are concatenated or combined into one string. </li>
</ul>

<hr/>

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dpevccu4dm2q2j4snl04.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ibe9wl0vjru3kocwbldk.png)
<hr/>
*strchr()*
<ul>
    <li>strchr() searches a given string for a specified character. </li>
    <li>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. </li>
</ul>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kcmtvqxqf9gcih7sdxg8.png)
<ul>
   <li> 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. </li>
   <li> Then, we create a conditional. If we can't find it, print "No results are found...", else, print this statement. </li>
</ul>

<hr/>

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpi4m61srr0drpohdtr8.png)

<ul>
    <li>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. </li>
</ul>

<hr/>

*strtok()*
<ul>
   <li> strtok() is used for tokenizing a string. </li>
   <li> A token is a sequence of characters within a string that is bound by a delimiter.</li>
   <li> A delimiter is something like a comma, period, or space. </li>
   <li> Breaking a sentence into words is referred to as "tokenizing" </li>
   <li> strtok() takes in 2 arguments. The 1st argument is the string that is being tokenized. The 2nd argument is all the possible delimiters. </li>
</ul>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umn90gec34zjpasfbp7c.png)
<ul>
    <li> Here, we create a string with delimiters. </li>
    <li> We set the delimiter to a variable. </li>
    <li> Then, we call strtok() and print it to the console. </li>
    <li> The end result is all the delimiters from the token are removed. </li>
</ul>


<hr/>

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kp83q68m9ue6b3whmst1.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzdvk61i9we3ovxc2zuz.png)

<hr/>

*atof()*
<ul>
    <li>This converts a string to a float.</li>
    <li> There are many different variations with this. </li>
    <li> atoi() converts a string to an integer.</li>
    <li> atol() converts a string to a long. </li>
    <li> atoll() converts a string to a long long. </li>
</ul>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xz8nw1kdwrovkdgic87.png)

<hr/>

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!