string and Character array

string is a sequence of characters that is treated as a single data item and terminated by null character '\0'. Remember that C language does not support strings as a data type. A string is actually one-dimensional array of characters in C language. These are often used to create meaningful and readable programs.

For example : The string “hello world” contains 12 characters including '\0' character which is automatically added by the compiler at the end of the string.


Declaring and Initializing a string variables

There are different ways to initialize a character array variable.

char name[13]="StudyTonight";   //valid character array initialization

char name[10]={'L','e','s','s','o','n','s','
char name[13]="StudyTonight";   //valid character array initialization
char name[10]={'L','e','s','s','o','n','s','\0'};    //valid initialization
'}; //valid initialization

Remember that when you initialize a character array by listings all its characters separately then you must supply the '\0' character explicitly.

Some examples of illegal initialization of character array are,

char ch[3]="hell";   //Illegal

char str[4];
str="hell";   //Illegal  

String Input and Output

Input function scanf() can be used with %s format specifier to read a string input from the terminal. But there is one problem with scanf() function, it terminates its input on first white space it encounters. Therefore if you try to read an input string “Hello World” using scanf() function, it will only read Hello and terminate after encountering white spaces.

However, C supports a format specification known as the edit set conversion code %[..] that can be used to read a line containing a variety of characters, including white spaces.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char str[20];
 clrscr();
 printf("Enter a string");
 scanf("%[^\n]",&str);
 printf("%s",str);
 getch();
}

Another method to read character string with white spaces from terminal is gets() function.

 char text[20];
 gets(text);
 printf("%s",text);

String Handling Functions

C language supports a large number of string handling functions that can be used to carry out many of the string manipulations. These functions are packaged in string.h library. Hence, you must include string.hheader file in your program to use these functions.

The following are the most commonly used string handling functions.

Method Description
strcat() It is used to concatenate(combine) two string
strlen() It is used to show length of a string
strrev() It is used to show reverse of a string
strcpy() Copies one string into another
strcmp() It is used to compare two string

strcat() function

strcat("hello","world");

strcat() function will add the string “world” to “hello”.


strlen() function

strlen() function will return the length of the string passed to it.

int j; 
j=strlen("studytonight");
printf("%d",j);
output : 
12

strcmp() function

strcmp() function will return the ASCII difference between first unmatching character of two strings.

int j; 
j=strcmp("study","tonight");
printf("%d",j);
output: 
-1

 
Scroll to Top