How to work with strings in C Programming5 min read


A string is a collection of characters which is treated as a single data item. A group of characters enclosed in double quotes is known as a string constant. Some of the examples of string constants are:

  1. “hai”
  2. “hello world”
  3. “My name is suresh”

In C programming, there is no predefined data type to declare and use strings. So, we use character arrays to declare strings in C programs. The common operations that can be performed on a string are:

  • Reading and writing strings
  • Combining strings together
  • Copying one string to another
  • Comparing strings for equality
  • Extracting a portion of the string (substring)

Declaring and Initializing Strings

Like variables, we should declare strings before using them in the program. Since we already known that strings are implemented as character arrays, the syntax for declaring a string.

The size refers to the number of characters in the string. When the compiler assigns a character string to a character array, it appends a ‘\0’ to the end of the array. So, the size of the character array should always be number of characters plus 1.

Like numeric arrays and variables, character arrays can also be initialized when they are declared.

If less number of characters are provided than the size of the string, the rest of the characters are initialized to ‘\0’. If we try to assign more characters then the size of the string, compiler gives an error.

Note: The string termination character ‘\0’, is used to terminate a string. In C, there is no data type provided available. We maintain strings using character arrays. A string is a variable length structure stored inside a fixed size array. The size of the array is often larger than the number of characters in the string. So, the compiler must have some means to detect the end of the string. For this purpose we use ‘\0’.

Note: If ‘\0’ is not provided then the compiler will treat the array as a normal character array. Only when we provide the ‘\0’ character, compiler treats it as a string.

Reading Strings

Using scanf function

Strings can be read from the terminal by using the familiar scanf function. The format specifier to read strings is %s. An example of reading a string usingscanf function .

There is a downside of using scanf function for reading strings. The downside is, scanf cannot read strings with white spaces. For example, if we provide “New Delhi” as the string, scanf will read only “New” and the rest of the characters are neglected or not processed. So, to read “New Delhi”, we have to declare two character arrays and read “New” and “Delhi” separately.

Reading and printing a line of text

Reading Text (getchar and gets)

As we have learned that scanf function cannot be used for reading a line of text, we have to search for other alternatives for reading a line of text which has white spaces embedded in it. One alternative is to read the line of text character by character until the enter key (\n) is pressed. Here we will use a predefined function called getchar( ) which reads a single character from the terminal.

In the above program we are reading characters until a new line character (\n) is encountered. So, by using the above approach we can read a line of text. There is a more efficient way for reading a line of text with white spaces. We can use the gets function which is available in the stdio.h header file. The purpose of gets function is to read a line of text from the terminal/keyboard.

The arrayname in the above piece of code is the array in which we are going to store the string. By using the gets function the above program.

Printing Text (putchar and puts)

We can print strings in multiple ways. First is by using printf function. We can use the format specifier %s to print a string.

The output of the above piece of code is “hai”. The second way is to print the string character by character. For this purpose we can use the format specifier%c for printing each character or we can use the predefined function putchar.

In the above code, ch is the character that we want to print on the screen. By using the putchar function we can print a line of text or string as shown below:

The last alternative for printing a string or a line of text is by using the predefined function puts. This function is available in the stdio.h header file. By using the puts function we can rewrite the above program as shown below:

Take your time to comment on this article.

Leave a Comment