Introduction
In C programming, strings are fundamental for working with textual data. Unlike some other languages, C handles strings using character arrays with a null terminator. String handling in the C programming language typically involves the use of character arrays (strings) and a set of functions provided by the standard C library (<string.h>). A C string is essentially a one-dimensional character array. Each character in the string occupies an element in the array. The key difference is a special character '\0' (null character) at the end of the string. This null character signifies the string's termination.
Declaring C Strings
char name[50]; Here, the name can hold up to 49 characters (excluding the null terminator).
Input and Output in String Handling
Strings can be read from the standard input (keyboard) or written to the standard output (console) using scanf() and printf() functions respectively, or their variants like gets() and puts() as well.
For example
Consider a Program in C Language to implement all String Functions.
Source Code
Output
![String function in C Output]()
Code Description
This program implements custom versions of some common string functions (strlen, strcpy, strcat, strcmp, and strrev) along with the usage of standard library functions for comparison. It takes two strings as input from the user and demonstrates the working of these functions.
The length of the first string "Shikha" is 6 characters. The copy of the first string is "Shikha". The concatenation of the strings "Shikha" and "Tiwari" is "ShikhaTiwari". The comparison of the strings "ShikhaTiwari" and "Tiwari" results in a negative value (-1). The length of the concatenated string "ShikhaTiwari" is 12 characters (including the null terminator). The custom copy of the first string "Shikha" is "ShikhaTiwari". The custom concatenation of the strings "Shikha" and "Tiwari" is "ShikhaTiwariTiwari". The custom comparison of the strings "ShikhaTiwariTiwari" and "Tiwari" results in a negative value (-1). The reversed string of "ShikhaTiwariTiwari" is "irawiTahkihS".
Summary
C strings are powerful for text processing, but they require manual memory management. To find the length of a string, you can use the strlen() function. To copy one string to another, you can use the strcpy() function. To concatenate (append) one string to another, you can use the strcat() function. To compare two strings, you can use the strcmp() function.