Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: C Programming. No Comments on Strings in C Programming

This article provides a comprehensive overview of strings in C programming language along with example programs.

 

Strings

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 is as shown below:

 

string-decl

 

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. Some of the examples for initializing the string are as shown below:

 

strings-examples

 

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 using scanf function is shown below:

 

reading-string-ex

 

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.

 

/* C program to read a line of text using getchar function */
#include<stdio.h>
#include<conio.h>
main()
{
    char line[20], ch;
    int i;
    clrscr();
    printf("Enter the line of text: ");
    i = 0;
    do
    {
        ch = getchar();
        line[i] = ch;
        i++;

    }while(ch != '\n');
    line[i] = '\0';
    printf("%s",line);
    getch();
}

 

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 usage of gets function is as shown below:

 

gets-syntax

 

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 can be rewritten as shown below:

 

/* C program to read a line of text using gets function */
#include<stdio.h>
#include<conio.h>
main()
{
    char line[20], ch;
    int i;
    clrscr();
    printf("Enter the line of text: ");
    gets(line);
    printf("%s",line);
    getch();
}

 

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. Let us consider the following example:

 

print-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. The usage of this function is as shown below:

 

putchar-syntax

 

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:

 

/* C program to print a string using putchar function */
#include<stdio.h>
#include<conio.h>
main()
{
    char line[20];
    int i;
    clrscr();
    printf("Enter the line of text: ");
    gets(line);
    i = 0;
    while(line[i] != '\0')
    {
        putchar(line[i]);
        i++;
    }
    getch();
}

 

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:

 

/* C program to print a string using puts function */
#include<stdio.h>
#include<conio.h>
main()
{
    char line[20];
    clrscr();
    printf("Enter the line of text: ");
    gets(line);
    puts(line);
    getch();
}

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *