What is string in C- Definition, Syntax, Example and Program

What is string in C- Definition, Syntax, Example and Program

 String in C programming

The string is C the set of characters, digits, and symbols. It is defined as an array of the character. That is they are character arrange one after another in memory. Thus, the character array is also known as a string. Each character within the string will be stored within one element of the array. A string is always terminated by a null character (\0). The size of a character string represents the maximum number of characters that the string can hold

Syntax of string in C programming

      char string_name[string_size];

As in the above syntax, char refers to the character data type that will be stored in the array, string _name is the name of the string variable and string_size indicates the number of characters that can be stored inside the array.

Example of string in C programming

char name[15];

char name[15][20];

This declares the name as a string variable that can hold a maximum of 15 characters.

A string is simply a sequence of characters, so declare a string, we have to put data type char with string name with square.

Initialization of string in C programming

Let us take a string

   char name[10]={'a', 'e', 'i','o',' u',  'v',  '+', '-', '*','\0'};

or second method

char name[10]= “Welcome”

Each character is treated as an element of the array name and is stored in the memory as following

When the compiler sees a character string, it terminates it with an additional null character means empty. Thus, the element namep[9] holds the null character ‘\0’ at the end. When declaring character arrays, we must always allow one extra element space for the null terminator.

Write a C program to input character-wise in array and display them

#include<stdio.h>

#include<conio.h>

void main()

{

                char name[10]={'a', 'e', 'i','o',' u',  'v',  '+', '-', '*','\0'};

                printf("\n Given string =%s",name);

    getch();           

}

Output

Write a C program to input character-wise in array and display them

Let see another example

Write a C program to input string in an array and display them

#include<stdio.h>

#include<conio.h>

void main()

{

                char name[10]= "Welcome";

                printf("\n Given string =%s",name);

    getch();           

}

 Output

Write a C program to input string in an array and display them



Array of string

The two-dimensional array of characters is called an array of strings. It is also known as the table of strings. It is very useful for solving strings sorting problems. In another way, an array of strings can be declared and handled like two-dimensional arrays.

Syntax of array of string

            char string _name[MAX][SIZE];

As in the above syntax, char refers to character data type, string_name is the name of the string variable and in a two-dimensional array, [MAX] indicates the number of string, and [SIZE] indicates the maximum number of characters associated with the string.

Example of array of string

     char name[5][10];

In this example, here is a two-dimensional character array. The array is initialized with five character strings with a maximum size of ten.

Program to demonstrate table of string

#include<stdio.h>

#include<conio.h>

void main()

{

                char name[5][10]= {"Niraml","Book","Copy","Home","Enjoy"};

                int i;

                for(i=0;i<4;i++)

                {

                printf("\n %s",name[i]);

    }

    getch();           

}

Output



How to take string as an input for the user

In string, we can also use scanf() function to take string input from the users. Also gets()function used to read a string from the users and puts() function is used to display string as well

For Example

Write a C program to take input from the user and display (Using scanf() and printf() function)

#include<string.h>

#include<conio.h>

void main()

{

                int name[10];

                printf("\n Enter the string");

                scanf("%s", name);

                printf("\n Given string is %s",name);

                getch();

}

Note:- Ampersand (&) doesn't use while taking string input. Such as scanf("%s", name);

Output



By using gets() and puts() function

Write a C program to take input from the user and display

#include<string.h>

#include<conio.h>

void main()

{

                char name[10];

                printf("\n Enter the string");

              gets(name);

                puts(name);

                getch();

}

 Output



Here we use gets() function to take string name from the users. Here name variable can only handle ten characters. Similarly, the puts() function is used to display the name string. In this example user gives Hari as input which is taken by the gets() function and display Hari by using puts()function. We must use #include<conio.h> header file to use gets() and puts() function.

We can take much string from the user as flowing 

Example

Write a c program to read five-string and display them

#include<stdio.h>

#include<conio.h>

void main()

{

                char name[5][15];

                int i;

                printf("\n Enter five string");

                for(i=0;i<5;i++)

                {

                                scanf("%s", name[i]);

                }

                for(i=0;i<5;i++)

                {

                                printf("\n %s", name[i]);

                }

                getch();

}


Output

 




Also, check

String manipulation function

Click here

 

 

 

  

Post a Comment

0 Comments