String Handling


What is String?

String is a collection of characters.
In terms of C language we can say that a string is an array of characters.
String is always enclosed between double quatation mark.

How to declare string?

In C language there is no separate DataType to declare string. We have to create an array of character to create a string. Because string is a collection of characters.
Syntax:
char StringName [Size];
Here, Size indicates number of characters that can be stored in a string.
Compiler will automatically insert NULL character at the end of string to indicate end of string.
So while declaring the character array the size of an array must be one more than the number of characters in the string. It is because the last character in the string is NULL character.
Example:
char Name[10];
Here, Name is a string which can store 9 characters inside it.

Reading String

String can be read using either scanf () function or using gets () function.

     
Function Purpose
scanf()

scanf() function is used to read string from keyboard. 
Syntax
scanf("%s", StringName);
scanf () function stops reading characters from keyboard upon receiving whitespaces. So you can not enter a line of text containing white spaces using scanf () function.
using scanf () function we can read one or more then one string at a time.

gets ()

gets() function is used to read string from keyboard.
Syntax:
gets(StringName); 
There is no need to specify format specifier in gets () function. 
Using gets () function we can read a line of text that contains white spaces.
Using gets () funcion we can read only one string at a time.


Display String

String can be display using either printf () function or puts () function.

Function Purpose
printf()

printf () function is used to display string. 
Syntax
printf("%s", StringName);
Using printf () function we can display  one or more then one string at a time.

puts ()

puts() function is used to display string.
Syntax:
puts("String Message" or StringName); 
Using puts () function we can display only one string at a time.
There is no need to specify format specifier in puts () function.  


Download Projects


Download Programs