String — An array of characters with ‘\0’
--
Numbers and characters can be stored in an array. What if a word or a sentence is needed to be stored in a variable? One way to do this is using a character array. There is a more convenient way, to store them as strings.
What is a string?
A string is an array of characters that is terminated with a null terminator. The null terminator is a special character ‘\0’, which has the numerical value of 0. It is used to represent emptiness. So, in the case of a string, the null terminator denotes where the string ends. So, can’t we just use an empty char to terminate? No, char cannot accept empty values. It does not work like that. That is why a null terminator is used.
char myself[] = {'I', ' ', 'a', 'm', ' ', 'c', 'o', 'o', 'l'};
printf("%c", myself[0]);
Output:
I
This is how to store a sentence or a word using a char array. Now, think what if there is a need to display the whole sentence. The way is to create a for loop and loop through each char and display them. This is inconvenient when working with many words or when you need to compare whether two strings are equal.
char myself[] = {'I', ' ', 'a', 'm', ' ', 'c', 'o', 'o', 'l', '\0'};
printf("%s", myself);
Output
I am cool
A string is stored like a char except for the ‘\0’ at the end. A string can be displayed using the “%s” format specifier. Now, the looping of the array is not needed to display the whole sentence. This is the actual format of the strings but declaring strings is easier.
char myself[] = "I am cool";
printf("%s", myself);
This too gives the same output. While declaring a string like this, the string is converted to a char array and terminated with ‘\0’ behind the scenes. So, it’s cool.
Comparing strings
To compare strings, we need to include ‘string.h’.
#include <string.h>
This header file provides some functions or ways to do some operations on strings. The ‘strcmp()’ is used to compare strings.
strcmp(string1, string2);
Within the parentheses of the ‘strcmp()’, the strings to be compared are provided. This outputs 0 if the strings are equal and some other values if the strings are not equal.
Output
0
Assigning to a string
String assignment does not work in the usual way.
char myCar[] = "BMW";
char myNewCar[] = "Tesla";myCar = myNewCar;
printf("%s", myCar);
Output
main.c:16:11: error: assignment to expression with array type
The error says we are trying to assign to an expression with an array type. Basically, a string is an array of characters. So, we have to make a loop to assign each item of the myCar array to the item of myNewChar array. We cannot assign it directly. We need to use a function from ‘string.h’ that is strcpy().
To assign a string to another string pass the destination string and the source string in the order, ie strcpy(destination string, source string).
#include <stdio.h>
#include <string.h>int main()
{
char myCar[] = "BMW";
char myNewCar[] = "Tesla";
strcpy(myCar, myNewCar);
printf("%s", myCar);
}
Output
Tesla
Reading strings
Reading string input from the user is a bit different.
char name[30];
scanf("%s", name);
Usually, an ampersand is placed before the variable to which the input value is to be stored.
int num;
scanf("%d", &num);
For strings, the ampersand is not needed. The reason for this is beyond the scope of this article. You can search about it if you are interested.
This is how to read strings. It works fine for words without spaces. In sentences and full names, there will be spaces.
char name[30];
printf("Enter name: ");
scanf("%s", name);
printf("%s", name);
If the name is one word like ‘Shafi’, it outputs “Shafi”. If the name contains space like “Shafi Sahal”, still the output will be “Shafi”. What happened here is that while reading the input, the reading stopped at the first occurrence of the space in the string.
To read strings with spaces, there is more than one way. Here, we will be discussing the way by using the scanset. You can check other ways if you are interested. You may also check what is a scanset?
char name[30];
printf("Enter name: ");
scanf("%[^\n]", name);
printf("%s", name);
Output
Enter name: Shafi Sahal
Shafi Sahal
Here, instead of a format specifier, the scanset is given. The scanset tells how to read the input. The scanset is given between the square brackets: %[], in this case, the scanset is “^\n”. This tells to read the input until a newline is encountered. So, until the enter key is pressed, the whole sentence or words will be taken as the input.
Sample Quiz Program
Let’s make a small quiz program from all the knowledge we acquired through this and previous articles.
Previous => Arrays — store multiple items using the same variable.