String and Character Array
String is a sequence of characters that is treated as a single data item and terminated by null character (\0). A string is actually one-dimensional array of characters in C language. These are often used to create meaningful and readable programs.
Example :- “INTEGRAL UNIVERSITY”
Declaring and Initializing a string variables :
There are two ways to declare a string in c language.
1. By char array :
char ch[10]={'l', 'u', 'c', 'k', 'n', 'o', 'w', '\0'};
2. By string literal :
char ch[ ]="lucknow";
Arrays and strings : We have already read this topic.
String Manipulation
C supports a string handling library which provides useful functions that can be used for string manipulations.
All these string handling functions are defined in the header file string.h. So every time we use these string handling functions string.h header file must be included.
Example :
Finding total number of characters in a string, we use the strlen() function.
Concatenate two string, we use the strcat() method.
Comparing two strings, we use the strcmp() method.
String functions
Function |
Work |
strcat( ) |
concatenates two strings |
strlen( ) |
finds the length of string |
strcpy( ) |
copies one string in to another another string |
strcmp( ) |
compares two strings |
strlwr( ) |
converts string to lowercase |
strupr( ) |
converts string to uppercase |
strstr( ) |
finds first occurrence of substring in string |
strtok( ) |
splits strings into tokens |