How to represent a string in the C language?
In the C language, strings can be represented using either character arrays or pointers.
- Representing a string using a character array:
char str[] = "Hello World";
Here, the variable ‘str’ is an array of characters that contains each character of the string “Hello World” and is terminated by a null character (‘\0’). Specific characters can be accessed by index, for example, ‘str[0]’ represents the first character of the string.
- Representing strings using character pointers:
char *str = "Hello World";
The variable str here is a pointer to a character, pointing to the first address of the string “Hello World”. Using the pointer, we can access specific characters such as *(str + 0) or str[0] to represent the first character of the string. It is important to note that the string pointed to by the character pointer is typically a constant string and cannot be modified through the pointer.
In addition, there are library functions in C language for manipulating strings, such as strlen() for getting the length of a string, strcpy() for copying strings, strcat() for concatenating strings, etc. These functions require including the