- A String is an array of character.
- We can easily represent a string using one dimension array.
- Similar to an array of integer that allows storing collection of integer values, we can also store string and represent string using an array of character.
- We can think of a string as an array that can hold individual characters on different index.
- A specific character in a string is accessed by an index.
- Array index always starts from 0 and ends to n-1 that means first character of a string will be stored on index 0, second character on index 1 and so on. The last character will be ‘\0’ (NULL) into the string.
- In C, array elements are allocated contiguous memory blocks.
- String or character arrays can be represented by the following ways.
- Single -Dimension Arrays (String)
- Two- Dimension Arrays (Array of string)
Single -Dimension Arrays (String)
- “An array of character is known as the one-dimensional character array or string”
Declaration and Initialization of String:
- We can declare string using one dimension character array using the following syntax:
char string-var-name [size] ;
- string-var-name specifies name of the string to refer it. It can be any valid and meaningful name and it should follow all the naming convention rules.
- Size defines number of maximum characters it can hold.
- Size is defined inside the ‘[]’ brackets.
- In C89, the size of an array must be specified using a constant expression. Thus, in C89, the size of an array is fixed at compile time. (C99 allows arrays whose sizes are determined at run time).
- For example, to declare string called name use this statement:
char name[6] = { ‘S’, ‘A’, ‘M’, ’M’, ’Y’, ’\0’ };
OR
char name [] = { ‘S’, ‘A’, ‘M’, ’M’, ’Y’, ’\0’ };
OR
char name [] = { “SAMMY” } ; OR char name [ ] = “SAMMY” ;
- Above string will be stored in memory as follow:
- Note that last character in string is ‘\0’ (NULL) character. It is used to identify end of the string
- If we don’t specify null value at the end of the string, compiler will automatically insert null value at the end of the string.
Accessing string Elements:
- We can access each character of string by using its index within square brackets along with string name.
- For example, to access the first character of the string name, you can use name [0], for second element namd [1], and so on.
Dynamic initialization:
- We can also initialize string at run time. It is referred to as dynamically initialization.
- The following example uses for loop to initialize marks array from user at run time.
char name[15] ;
printf(“\n Enter Name :”);
scanf(“%s”, &name); // OR gets(name); (Need to include string.h)
- Note that by default scanf() scans string only till it encounters space. As soon as it founds space, it stops scanning.
- gets() can read string till it encounters null value so it is more suitable while reading long string.
- The following program demonstrates reading string from user and displaying on the screen character by character.
/* Program to demostrate reading a string from the terminal. */
#include "stdio.h"
#include "string.h"
void main(void)
{
char str[50]; /* this declares string 0f 50 cahracters*/
int i=0;
clrscr();
/* read string from the keyboard */
printf(“\n Enter String:”);
gets(str);
while ( str[i]! = ‘\0’ ) /* display contents of str */
{
printf(“%c ”, i ) ;
i++;
}
}
- The above program can be replaced with the following code fragment to read a string.
char ch;
/* this causes count to be overrun */
while ( ch = getchar() ! = ‘\0’)
printf(“%c”, ch);