Strings in C: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-05 03:43:22   12343  Share
Subject Syllabus DetailsSubject Details 7 Questions 17 Program
☰ TContent
☰Fullscreen

Table of Content:

We can store a group of integers in an integer array, similarly, a group of characters can be stored in a character array.

A string constant or a String is a one-dimensional array of characters terminated by a null ( ‘\0’ ). This null character indicates the end of the string. Strings are always enclosed by double quotes. Whereas, a character is enclosed by single quotes in C.

In C, a string can be referred either using a character pointer or as a character array.

The following declaration and initialization create a string consisting of the word "atnyla". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "atnyla"

char stng[7] = {'a', 't', 'n', 'y', 'l', 'a', '\0'}; 

you can write the above statement as follows ?

char stng[] = "atnyla";

you do not place the null character at the end of a string constant. The C compiler automatically places the '\0' at the end of the string when it initializes the array.

Example C String:

Difference between above declarations are, when we declare char as string[11], 11 bytes of memory space is allocated for holding the string value.

 char string[11] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '\0'};
                   
				   (or)

 char string[11] = "helloworld"; 
  
 

When we declare char as string[], memory space will be allocated as per the requirement during execution of the program.

 
char string []    = "helloworld"; 
 

String in C Example

Program

 #include
 
int main ()
{
  char string1[11] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '\0'};
   char string2[11] = "helloworld";
   char string3 []  = "helloworld"; 
 
   printf("The string is : %s \n", string1 );
   printf("The string is : %s \n", string2 );
   printf("The string is : %s \n", string3 );
   return 0;
}
 

Output

 The string is : helloworld
The string is : helloworld
The string is : helloworld
Press any key to continue . . .
 

scanf() to read a string

This C program illustrates how to read string from terminal.

Program

#include
int main()
{
    char name[20];
    printf("Enter Your name: ");
    scanf("%s", name);
    printf("Your name is %s.", name);
    return 0;
}
 

Output

 Output 1:
Enter Your name: atnyla
Your name is atnyla. 

Output 2:
Enter Your name: Rambo Azmi
Your name is Rambo. 
 

Here, program ignores Azmi because, scanf() function takes only a single string before the white space, i.e. Rambo.

Reading a line of text Using getchar()

This program reads a full line of text and store each character one by one.

Program

#include 
int main()
{
    char line[30], ch;
    int i = 0;
    printf("Enter name: ");
    while(ch != '\n')    // terminates if user hit enter
    {
        ch = getchar();
        line[i] = ch;
        i++;
    }
    line[i] = '\0';       // inserting null character at end
    printf("Line is: %s", line);
    return 0;
}
 

Output

Enter name: Hello reader how are you?
Line is: Hello reader how are you?
Press any key to continue . . .
 

In the program above, using the function getchar(), ch gets a single character from the user each time.

This process is repeated until the user enters return (enter key). Finally, the null character is inserted at the end to make it a string.

Reading a line of text Using standard library function

Program

To make life easier, there are predefined functions gets() and puts in C language to read and display string respectively.

#include
int main()
{
    char line[30];
    
    printf("Enter name: ");
    gets(line);     //Function to read string from user.
    
    printf("Name: ");
    puts(line);    //Function to display string.
    return 0;
}
 

Output

Enter name: welcome to atnyla!
Name: welcome to atnyla!
Press any key to continue . . .