Strings in c
Introduction
String in C language is an array
of characters that is terminated
by \0 (null character).
C language does not support
strings as a data type.
There are two ways to declare
string in c language:
By char array
By string literal
Example of declaring string by
char array:
To hold the null character at the
end of the array, the size of the
character array containing the
string is one more than the number
of characters in the word "Hello."
Following is the memory
presentation of the above defined
string in C−
While declaring string, size is not
mandatory.
Actually, you do not place
the null character at the end of a string
constant. The C compiler automatically
places the '\0' at the end of the string
when it initializes the array.
You can also define string by string
literal in C language. For example:
Insuch case, '\0' will be appended at
the end of string by the compiler.
Example
gets() and puts() functions
The gets() function reads string
from user and puts() function
prints the string.
Both functions are defined in
<stdio.h> header file.
Example
String Manipulation Functions
There are many important string
functions defined in "string.h"
library.
String Length: strlen() function
The strlen() function returns the
length of the given string.
It doesn't count null character '\
0'.
Copy String: strcpy()
The strcpy(destination, source)
function copies the source string
in destination.
String Concatenation: strcat()
The strcat(first_string,
second_string) function
concatenates two strings and
result is returned to first_string.
Compare String: strcmp()
The
strcmp(first_strin
g, second_string)
function
compares two
string and returns
0 if both strings
are equal.
Reverse String: strrev()
The strrev(string) function
returns reverse of the given
string.
String Lowercase: strlwr()
The strlwr(string) function returns
string characters in lowercase.
String Uppercase: strupr()
The strupr(string) function
returns string characters in
uppercase.