Strings
In C language, strings are stored in an array of character (char) type along with the null terminating character “\O” at the end.
char name [ ] = {‘K’, ‘R’ , ’I’, ‘S’, ‘H ‘ , ‘A’, ‘\O’};
‘\O’= Null character whose ASCII value is 0.
‘ 0’ = ASCII value is 48.
In the above declaration ‘\0’ is not necessary. C inserts the null character automatically.
# include <stdio.h>
void main ( )
{
char name ] = “RAM”;
printf (“%s”, name);
}
% s = It is used in printf ( ) as a format specification for printing out a string.
All the following notations refer to the same element
name [i] *(name+i)
* (i + name)
q [name]
# include <stdio.h>
void main ( )
{
char name [ ]= “shyam”;
char * ptr;
ptr = name; / *store base address of string while (*ptr ! = \O’ )
{
printf (“%c”, *prt);
ptr + +;
}
}
Note The above program is used to print all the characters of an string using pointer.
Length of String
Use strlen ( ) function to get the length of a string minus the null terminating character.
Syntax int strlen (string);
Concatenation of String
The strcat( ) function appends one string to another.
Syntax char * strcat (string 1, string 2);
Copy String
To copy one string to another string variable, we use strcpy( ) function.
Syntax strcpy (string 1, string 2);