What is String?

A string is defined as a sequence of characters or group of characters.
A string is always enclosed between double quotation marks.
In terms of programming languages a string is defined as an array of character. Because an array is a collection of variables of same data type and string is a collection of characters.
We can store letters (A-Z, a-z), digits (0 – 9) and special characters such as +, -, *, %, /, () [] {} $ # & , . ? “ “ ‘ ‘ @ etc into string.


Representation of string

A string can be represented in the form of array. Because C and C++ supports only character data type and string is a collection of characters. So in order to represent the string we need to declare an array of character.
We can declare an array to represent the string as shown below:
char StringName [Size];
StringName is the name of the array which holds the value for the string.
Size indicates number of characters that can be stored in the string.
Consider the following example:
Suppose we want to declare a string that can holds the name of the student. We can declare the string as shown below:
char Name [10];
The string we just declare above can store only 9 characters even though the size of the string is of 10 characters. It is because of the fact that the last character in the string is always NULL (‘\0’).
NULL character is automatically appended at the end of each string to indicate end of the string. The string that ends with the NULL character is also known as NULL terminated string.
So while declaring a string the size of the array must be always one greater then the number of characters in the string because the last character is NULL character.
The representation of the string in memory is shown in the figure given below:
char Name [10];
It will occupy 10 bytes in memory in the sequence.


Reading strings

Reading string means enter value for the string from keyboard. You can enter value of the string at runtime using scanf () or gets () functions as shown below:
char Name [10];
scanf (“%s”, Name);
gets (Name);

Writing strings

Writing means display value of the string. You can display the value of the string at runtime using printf () or puts () functions as shown below:
Printf (“Name = %s”, Name);
Puts (Name);

Download Projects

Download Programs