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','\0'}; // valid initialization
Remember that when you initialize a character array by listing all of 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 the 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<string.h>
void main()
{
char str[20];
printf("Enter a string");
scanf("%[^\n]", &str); //scanning the whole string, including the white spaces
printf("%s", str);
}
Another method to read character string with white spaces from terminal is by using the 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.h header file in your programs to use these functions.
The following are the most commonly used string handling functions.
Method | Description |
---|---|
strcat() | It is used to concatenate(combine) two strings |
strlen() | It is used to show length of a string |
| 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" i.e it will ouput helloworld.
strlen() function
strlen()
function will return the length of the string passed to it.
int j;
j = strlen("studytonight");
printf("%d",j);
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);
-1
strcpy() function
It copies the second string argument to the first string argument.
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50];
char s2[50];
strcpy(s1, "StudyTonight"); //copies "studytonight" to string s1
strcpy(s2, s1); //copies string s1 to string s2
printf("%s\n", s2);
return(0);
}
StudyTonight
strrev() function
It is used to reverse the given string expression.
#include<stdio.h>
int main()
{
char s1[50];
printf("Enter your string: ");
gets(s1);
printf("\nYour reverse string is: %s",strrev(s1));
return(0);
}
Enter your string: studytonight Your reverse string is: thginotyduts