The header file <string.h>
in C includes various string manipulation functions. Some common functions are strlen
, strcpy
, strcmp
, strcat
, strlwr
, and strrev
.
strlen
Function
The strlen
function returns the length of a string. It takes the string name as an argument. The syntax for using strlen
is:
n = strlen(str);
Here, str
is the name of the string, and n
is the length of the string returned by the strlen
function.
strcpy
Function
In C, you cannot simply assign one character array to another; you have to copy element by element. The string library <string.h>
contains a function called strcpy
for this purpose. The strcpy
function is used to copy one string to another. The syntax is:
strcpy(str1, str2);
Here, str1
and str2
are two strings. The content of string str2
is copied onto string str1
strcmp
Function
The strcmp
function compares two strings character by character and stops comparison when there is a difference in the ASCII value or the end of any one string. It returns the ASCII difference of the characters as an integer. A return value of zero means the two strings are equal, a negative value means the first string is less than the second, and a positive value means the first string is greater than the second. The syntax is:
n = strcmp(str1, str2);
Here, str1
and str2
are the two strings to be compared, and n
is the returned value of the differed characters.
strcat
Function
The strcat
function is used to join one string to another. It takes two strings as arguments; the characters of the second string are appended to the first string. The syntax is:
strcat(str1, str2);
Here, str1
and str2
are the two string arguments, and string str2
is appended to string str1
.
strlwr
Function
The strlwr
function converts upper case characters of a string to lower case characters. The syntax is:
strlwr(str1);
Here, str1
is the string to be converted into lower case characters.
strrev
Function
The strrev
function reverses the given string. The syntax is:
strrev(str);
Here, the string str
will be reversed.
In C, strings are groups of characters enclosed in quotation marks, declared as character arrays, and terminated with a '\0' (Null character). A character uses one byte in memory, whereas a single character string requires two bytes. Strings are declared with the char
data type followed by the array size in square brackets, e.g., char name[20];
Strings in C can be initialized using a character array, such as char name[8] = {'P', 'R', 'O', 'G', 'R', 'A', 'M', '\0'};
. Each character occupies 1 byte of memory, though the size can vary with different computer architectures. Characters in a string are stored in contiguous memory locations. The C compiler automatically inserts a null character (\0
) at the end of the string, making manual initialization of the null character optional.
You can set the initial value of a character array using a string literal. If the array is too small, the literal will be truncated; if the literal is smaller, the remaining characters are undefined. If no size is specified, the array size is set to the literal's length, including the null terminator.
Examples:
char str[4] = {'u', 'n', 'i', 'x'};
is valid but problematic because it's not null-terminated.char str[5] = {'u', 'n', 'i', 'x', '\0'};
is correct and null-terminated.char str[4] = "unix";
is problematic as it lacks space for the null-terminator.char str[] = "UNIX";
is correct as the compiler sets the appropriate size and adds the null-terminatorString constants
String constants (enclosed in double quotes) can be assigned to char
pointers or copied to char
arrays.
Example:
char *s = "hello";
char s[100];
strcpy(s, "hello");
Differences in Behavior
char *s = "hello";
, s
points to the string constant in the string constant table, making it read-only.char s[100]; strcpy(s, "hello");
, the string is copied into the array s
, making it modifiable.s = "hello";
is not allowed for arrays and will not compile./* Fragment 1 */
{
char *s;
s = "hello";
printf("%s\n", s);
// Output: hello
}
/* Fragment 2 */
{
char s[100];
strcpy(s, "hello");
printf("%s\n", s);
// Output: hello
}
Display of strings using different formatting
The printf
function with the %s
format is used to display strings. For example, printf("%s", name);
displays the entire string stored in name
.
To specify the accuracy and width, such as displaying the first 5 characters within a field width of 15, use:
printf("%15.5s", name);
Including a minus sign in the format (e.g., %-10.5s
) left-justifies the string:
printf("%-10.5s", name);
An array of strings in C is a table of multiple strings. Declaring it involves adding an additional dimension to specify the number of strings. The syntax is:
char array-name[rows][cols];
Example:
char names[5][10];
Here, names
is the array, 5
indicates the number of strings, and 10
specifies the maximum length of each string.
Another Example:
An array of strings can be declared and initialized as follows:
char names[3][10] = {"martin", "phil", "collins"};
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
m | a | r | t | i | n | \0 | |||
p | h | i | l | \0 | |||||
c | o | l | l | i | n | s | \0 |
The header file <string.h>
in C includes various string manipulation functions. Some common functions are strlen
, strcpy
, strcmp
, strcat
, strlwr
, and strrev
.
strlen
Function
The strlen
function returns the length of a string. It takes the string name as an argument. The syntax for using strlen
is:
n = strlen(str);
Here, str
is the name of the string, and n
is the length of the string returned by the strlen
function.
strcpy
Function
In C, you cannot simply assign one character array to another; you have to copy element by element. The string library <string.h>
contains a function called strcpy
for this purpose. The strcpy
function is used to copy one string to another. The syntax is:
strcpy(str1, str2);
Here, str1
and str2
are two strings. The content of string str2
is copied onto string str1
strcmp
Function
The strcmp
function compares two strings character by character and stops comparison when there is a difference in the ASCII value or the end of any one string. It returns the ASCII difference of the characters as an integer. A return value of zero means the two strings are equal, a negative value means the first string is less than the second, and a positive value means the first string is greater than the second. The syntax is:
n = strcmp(str1, str2);
Here, str1
and str2
are the two strings to be compared, and n
is the returned value of the differed characters.
strcat
Function
The strcat
function is used to join one string to another. It takes two strings as arguments; the characters of the second string are appended to the first string. The syntax is:
strcat(str1, str2);
Here, str1
and str2
are the two string arguments, and string str2
is appended to string str1
.
strlwr
Function
The strlwr
function converts upper case characters of a string to lower case characters. The syntax is:
strlwr(str1);
Here, str1
is the string to be converted into lower case characters.
strrev
Function
The strrev
function reverses the given string. The syntax is:
strrev(str);
Here, the string str
will be reversed.
Some other string functions are:
John Doe
5 min agoLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
ReplyJohn Doe
5 min agoLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Reply