7. Conquer String Manipulation.
Strings in C programming.
Strings are made of letters, numbers and other special characters such as punctuation marks etc. We typically use them in our programs for purposes such as debugging and displaying outputs in the console. But, unlike the other data types such as int, char, float and double, there is no dedicated data type that can hold a string in the C programming language. So we need to improvise.
So how are strings in C stored in programs?
This is where our friend arrays come in to help us. If you recall from our last tutorial on Arrays in C, we used an array to store a sequence of data (of the same type). Since a string is nothing but a set of characters, we use a char array to store our string in C, as a variable!
Declaring a String in C.
Since a string is also another char array, we can declare a string the usual way:
char string1[10];
By declaring a string, we’re letting the computer know that we are going to store a string in our array, string1 and it will have a length of 10.
NOTE: Remember to set the length of the string including the null character at the end. For example, if we need to declare a string that can hold the word ‘hello’, we need to make the string with a length of 6, Not 5. (5 for storing the actual word, +1 for storing the null character).
//to store the words 'hello world', we use following declaration
char string1[12]
The above code declares a string with a length of 12 (5 for the word ‘hello’, 1 for space, 5 for the word ‘world’ and another one for the ‘\0’ which is not visible).
Initializing a string.
To initialize a string, we can take multiple approaches.
char string1[] = "this is the string 1";
This is the most common way of initializing a string. When we use double quotes for the string, we do not need to worry about the null character (‘\0’) at the end because the C language automatically does it for us.
NOTE: make sure to not assign a string longer than the initial string. This is bad practice and can cause issues in your program later.
char string2[10] = "example";
This approach is also the same as the previous one, but this time we’re specifying the maximum length of our string. The actual content can be smaller than that. This way, we can assign a larger string to the same variable at a later time and will not cause any problems. But make sure that the maximum character length should not be exceeded.
If the length is 10, a maximum of 9 characters is allowed because the extra one will be reserved for the ‘\0’ at the end.
char string3[] = {'a', 'b', 'c', '1', '2', '3', '\0'};
This is a bit unusual way of initializing, but not uncommon among the developers. Notice how extra care is given to add the null character at the end of the string. This is what makes a string different from its counterpart, the character array. The regular character array would not have this special null character at its end.
printf("%s\n", string1);
Even though we used a loop to print an array last time, C language provides us with an easier way to print strings through the %s escape character. The \n is there to tell the program to go to the next line, so we can print all the strings, line-by-line.
Let’s do a quick test to see if you can remember the key points of strings in C language!
What can we do with strings?
Print a string:
Using the printf(“%s”, variable_name) we can print a string to the console. Just like in the previous example.
Read a string from the user – Method 1
We can ask a person to input a string, and store that string inside a variable using the scanf() function.
In this program, we’re declaring our string, and asking the user to input a string (a word), which we then print back onto the console. While we’re already familiar with how strings are used along with printf(), we can see a new function named scanf() to provide the input to the program.
scanf("%s", string1);
Similar to how we use printf() to produce outputs, we can use scanf() to provide inputs to our program. The double quotes represent the message to print and to expect what type of input, and in which order. In this case, “%s” means that it expects one string. To store the input string, we are providing the string1 argument to it.
NOTE: The problem with using scanf() is that it only accepts single words, not sentences. This is because scanf() only reads what we input until it meets a space. So, for example, if we input ‘hello world’, scanf() will read only the word ‘hello’.
Read a string from the user – Method 2
To remove the above issue with scanf(), we can use the fgets() function. Using this, we can input a complete string (i.e. a sentence) into our program.
This is a bit tricky code, since the fgets() function is a bit more complex than the scanf().
fgets(string1, 100, stdin);
The first parameter is string1, which is the variable we’re storing our string.
The second parameter tells the program how many characters it should read. This must be equal to or less than the actual length of the variable. (in this case, less than or equal to 100). As long as the string we enter is not longer than this number, we’re good to go!.
The third parameter is the hardest part to understand. For the time being, remember that this is the one you need to use if you use fgets() to input a string to your program.
puts(string1);
This is similar to printf(“%s”, string1), but a simpler way of printing a string. It does not have all the %s, %d and fancy double quotes, but just gets the job done, (and jumps to the next line after printing the string)
More advanced operations on strings
We know that strings in C are built upon the concept of arrays, and therefore require special care when manipulating them such as writing loops. But luckily, the C language has a special library named string.h, where there are multiple functions implemented for us to make our lives easier. let’s look at some of them in action, and discuss how we can use them to perform tasks like:
Obtaining the length of a string
Copying a string
Joining two strings (concatenation)
Let’s discuss some of the key statements in the code to have a better understanding.
#include
To use the special functions such as strcmp(), strlen(), we need to include a special file called ‘a library’. Without this library, the program will not compile.
int length = strlen(string1);
To calculate the length of a given string, we use strlen() function. It gives an integer, which is the length of the string. (NOTE: this length does not include the ‘\0’ at the end, therefore it’s easy to forget about it). We store that value in the variable ‘length’.
strcpy(string3, string1);
We use this function when we want to copy the contents from string1 to string3. It’s easy to forget about the order so be careful!
strcat(string1, string2);
Sometimes we need to add a string to the end of another string, and the strcat() function helps us to do exactly that. When used like this, the program will append the content in string2 to the end of string 1.
NOTE: using the functions on string.h library modifies the original strings. So if you want to keep the original strings intact, you will need to copy them into a temporary variable before modifying.
In this code, we perform
Copying the content from string1 into temp.
Join the temp and string2.
Print the three string variables to see what has changed.
In the above code, we can see that we’re actually modifying only the variable ‘temp’, and the other two variables (string1 and string2) are left intact.
Quiz time!
Exercises for you to try:
Now that we know how to manipulate strings, here are some exercises for you to try:
Write a program that reads two or three words from the user, and prints them as a single string (hint: use loops and strcat() function)
Write a program to find out if the entered word/string starts with a vowel/consonant (hint: check for the first element of the string i.e. myString[0])
Write a program to find the length of a string without using strlen() function (hint: use loops, the end of the string must be equal to ‘null’ or ‘\0’)