Untitled
Untitled
Untitled
String is sequence of characters that is treated as a single data item and terminated
by null character ‘\0’.Remember that C language does not support string as a data
type. A string is actually one dimensional array of characters in C language.
Declaring and Initializing string variables
The general form of declaration of a string variable
char stringname[size];
The size determines the number of characters in the stringname.
Example
char name[20];
char city[15];
Character array may be initialized when they are declared. There are different ways
to initialize a character array variable.
char name[ ]=“Jisha John”;
char name[10]={‘L’,’e’,’s’,’s’,’o’,’n’,’s’,’\0’};
Read string from terminal
1. scanf() function to read a string
The scanf() function reads a sequence of characters until it encounters a
white space.
Example
char name[20];
scanf(“%s”,name);
printf(“ Name : %s”,name);
2. gets() function to read a line of string and puts() function to display string.
Example
char name[20];
gets(name);
printf(“ Name : );
puts(name);
String handling functions
1. strlen()
strlen() function is used to find length of string. Length of string
means number of characters in the string.
Syntax
length = strlen(string);
Example
int len;
char array[20] = “MG University”;
len = strlen(array);