C String Functions
C strings are arrays of characters in the C programming language used to represent text. To manipulate and process C strings, C provides several string functions that are defined in the string.h
library. In this article, we will take a closer look at some of the most commonly used C string functions and provide practical examples to illustrate their use in C programming.
1. strlen()
Function
The strlen()
function is used to determine the length of a C string. The function takes a single parameter, which is a pointer to the C string, and returns the length of the string, excluding the null character ('\0').
Here's an example of using the strlen()
function:
1#include <stdio.h> 2#include <string.h> 3 4int main() { 5 char string1[] = "Hello, World!"; 6 int length = strlen(string1); 7 printf("The length of the string is: %d\n", length); 8 return 0; 9}
2. strcpy()
Function
The strcpy()
function is used to copy a C string. The function takes two parameters, the destination string and the source string, and copies the contents of the source string to the destination string.
Here's an example of using the strcpy()
function:
1#include <stdio.h> 2#include <string.h> 3 4int main() { 5 char string1[20]; 6 char string2[] = "Hello, World!"; 7 strcpy(string1, string2); 8 printf("The contents of the string1 are: %s\n", string1); 9 return 0; 10}
3. strcat()
Function
The strcat()
function is used to concatenate two C strings. The function takes two parameters, the destination string and the source string, and concatenates the source string to the end of the destination string.
Here's an example of using the strcat()
function:
1#include <stdio.h> 2#include <string.h> 3 4int main() { 5 char string1[20] = "Hello, "; 6 char string2[] = "World!"; 7 strcat(string1, string2); 8 printf("The contents of the string1 are: %s\n", string1); 9 return 0; 10}
4. strcmp()
Function
The strcmp()
function is used to compare two C strings. The function takes two parameters, the two strings to be compared, and returns an integer value indicating the result of the comparison. If the two strings are equal, the function returns 0. If the first string is greater than the second string, the function returns a positive integer value. If the first string is less than the second string, the function returns a negative integer value.
Here's an example of using the strcmp()
function:
1#include <stdio.h> 2#include <string.h> 3 4int main() { 5 char string1[] = "Hello, World!"; 6 char string2[] = "Hello, World!"; 7 int result = strcmp(string1, string2); 8 printf("The result of the comparison is: %d\n", result); 9 return 0; 10}
5. strlwr()
Function
The strlwr()
function is used to convert a C string to lowercase. The function takes a single parameter, which is a pointer to the C string, and returns a pointer to the converted string.
Here's an example of using the strlwr()
function:
1#include <stdio.h> 2#include <string.h> 3#include <ctype.h> 4 5int main() { 6 char string1[] = "HELLO, WORLD!"; 7 char *lower = strlwr(string1); 8 printf("The lowercase string is: %s\n", lower); 9 return 0; 10}
6. strupr()
Function
The strupr()
function is used to convert a C string to uppercase. The function takes a single parameter, which is a pointer to the C string, and returns a pointer to the converted string.
Here's an example of using the strupr()
function:
1#include <stdio.h> 2#include <string.h> 3#include <ctype.h> 4 5int main() { 6 char string1[] = "hello, world!"; 7 char *upper = strupr(string1); 8 printf("The uppercase string is: %s\n", upper); 9 return 0; 10}
C string functions are a powerful tool for processing and manipulating C strings. Whether you need to find the length of a string, copy or concatenate strings, compare two strings, or convert a string to lowercase or uppercase, C string functions have got you covered.