Definition:- A group of characters defined between double quotation marks is a string. (or) In C language The string is nothing but an array of characters and Terminated by a null character( \0 ).
Declaration:- char identifier[size]; ex:- char st[40];
Initialization:- char identifier[size]=”any string”; ex:- char st[]=”SHISHIR”;
Format specifier:- %s
When a compiler assigns a character string to a character array it automatically supplies a null character at the end of the string. Therefore the size should be equal to the maximum no of characters in the string + 1.
Declare and initialize a String
A string is a simple array with char as a data type. ‘C’ language does not directly support string as a data type. Hence, to display a string in ‘C’, you need to make use of a character array.
The general syntax for declaring a variable as a string is as follows,
char string_variable_name [array_size];
The classic string declaration can be done as follow:
char string_name[string_length] = "string";
The size of an array must be defined while declaring a string variable because it used to calculate how many characters are going to be stored inside the string variable. Some valid examples of string declaration are as follows,
char first_name[15]; //declaration of a string variable char last_name[15];
The above example represents string variables with an array size of 15. This means that the given character array is capable of holding 15 characters at most. The indexing of array begins from 0 hence it will store characters from a 0-14 position. The C compiler automatically adds a NULL character ‘\0’ to the character array created.
Let’s study the initialization of a string variable. Following example demonstrates the initialization of a string variable,
char first_name[15] = "SHISHIR"; char first_name[15] = {'S','H','I','S','H','I','R','\0'}; // NULL character '\0' is required at end in this declaration char string1 [6] = "hello";/* string size = 'h'+'e'+'l'+'l'+'o'+"NULL" = 6 */ char string2 [ ] = "world"; /* string size = 'w'+'o'+'r'+'l'+'d'+"NULL" = 6 */ char string3[6] = {'h', 'e', 'l', 'l', 'o', '\0'} ; /*Declaration as set of characters ,Size 6*/
In string3, the NULL character must be added explicitly, and the characters are enclosed in single quotation marks.
‘C’ also allows us to initialize a string variable without defining the size of the character array. It can be done in the following way,
char first_name[ ] = "SHISHIR";
The name of a string acts as a pointer because it is basically an array.
String Input: Read a String
When writing interactive programs which ask the user for input, C provides the scanf(), gets(), and fgets() functions to find a line of text entered from the user.
When we use scanf() to read, we use the “%s” format specifier without using the “&” to access the variable address because an array name acts as a pointer. For example:
#include <stdio.h> int main() { char name[10]; int age; printf("Enter your first name and age: \n"); scanf("%s %d", name, &age); printf("You entered: %s %d",name,&age); } Output:
Enter your first name and age: Shishir_Kant 32
The problem with the scanf function is that it never reads an entire string. It will halt the reading process as soon as whitespace, form feed, vertical tab, newline or a carriage return occurs. Suppose we give input as “Shishir Tutorials” then the scanf function will never read an entire string as a whitespace character occurs between the two names. The scanf function will only read Shishir.
In order to read a string contains spaces, we use the gets() function. Gets ignores the whitespaces. It stops reading when a newline is reached (the Enter key is pressed).For example:
#include <stdio.h> int main() { char full_name[25]; printf("Enter your full name: "); gets(full_name); printf("My full name is %s ",full_name); return 0; }
Output:
Enter your full name: Shishir Kant My full name is Shishir Kant
Another safer alternative to gets() is fgets() function which reads a specified number of characters. For example:
#include <stdio.h> int main() { char name[10]; printf("Enter your name plz: "); fgets(name, 10, stdin); printf("My name is %s ",name); return 0; }
Output:
Enter your name plz: Shishir My name is Shishir
The fgets() arguments are :
- the string name,
- the number of characters to read,
- stdin means to read from the standard input which is the keyboard.
String Output: Print/Display a String
The standard printf function is used for printing or displaying a string on an output device. The format specifier used is %s
Example,
printf("%s", name);
String output is done with the fputs() and printf() functions.
fputs() function
The fputs() needs the name of the string and a pointer to where you want to display the text. We use stdout which refers to the standard output in order to print to the screen.For example:
#include <stdio.h> int main() { char town[40]; printf("Enter your town: "); gets(town); fputs(town, stdout); return 0; }
Output:
Enter your town: Lucknow Lucknow
puts function
The puts function prints the string on an output device and moves the cursor back to the first position. A puts function can be used in the following way,
#include <stdio.h> int main() { char name[15]; gets(name); //reads a string puts(name); //displays a string return 0; }