1. String Constants and Character Arrays
1.1 What is a String Constant
Although C language does not have a string type, the concept of a string exists, which is a string constant: a sequence of zero or more characters ending with a NUL byte. String constants are immutable and are generally represented by a string of characters enclosed in double quotes (” “), such as:
“Hello!”, “\aWarning!\a”, “123abc\n”, “”
A string constant can be empty, such as “” which is an empty string constant, but even if it is empty, there is still a terminator NUL. (In C language, the escape character \0 is commonly used to represent NUL).
1.2 Relationship Between String Constants and Pointers
String constants are closely related to pointers because the value of a string constant actually represents the address of the memory space where these characters are stored, more accurately, the address of the first character in the string constant, not the characters themselves. Therefore, in C language, direct string assignment is not possible (because there is no string type). In C, a string is often accessed by declaring a pointer to char type and initializing it to a string constant:
char *message = "Hello World!";
// The above statement can also be split into the following two statements
char *message;
message = "Hello World!"; // This statement looks like string copying, but it is not, it only involves pointer operations
The above statement declares a pointer to char type and initializes it with the address of the first character in the string constant. The string constant can be accessed through the character pointer message:
#include <stdio.h>
int main()
{
char *message = "Hello World!";
printf("%s\n",message);
while(*message != '\0'){
printf("%c ",*message++);
}
printf("\n");
return 0;
}
/* output:
* Hello World!
* H e l l o W o r l d !
*/
This code uses a character pointer to traverse each character in the string constant.
1.3 Character Arrays
An array used to store characters is called a character array. In C language, apart from string constants, all other strings must be stored in character arrays or dynamically allocated memory. Defining a character array is the same as defining a normal array, the difference is that the character array stores character data:
char charArray[] = {'H','e','l','l','o'}; // Declare and initialize a character array
This statement defines and initializes a character array charArray. The actual length of this array is 6, because a string termination character ‘\0’ will be automatically added.
C language provides a more concise way to initialize character arrays:
char charArray[] = "Hello World!"; // Declare and initialize a character array
The above two declaration methods are equivalent.
Modifications can be made to a character array:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "hello world!";
int len = strlen(str);
int i;
for(i = 0; i < len; i++){
if( str[i] <= 'z' && str[i] >= 'a'){
str[i] = str[i] - 32;
}
}
printf("%s\n",str);
}
This code converts lowercase letters in the character array to uppercase letters and then outputs them.
1.4 Differences Between String Constants and Character Arrays
1) A string constant is a character array, but its content and length are fixed at initialization and cannot be changed; it can be accessed through a pointer to the first element of the string constant;
2) A character array is an array used to store characters, and the value of the character array can be changed.
2. Getting the Length of a String
The length of a string is the number of characters contained in this string, but this length does not include the NUL character. In C language, the library function strlen is used to calculate the length of a string:
size_t strlen(char const *string);
It should be noted the return type of strlen: size_t, which is an unsigned integer type.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str1[] = "Hello World!";
printf("%d\n",strlen(str1));
return 0;
}
/* output:
* 12
*/
3. Copying Strings
The library function strcpy is used in C language for string copying operations:
char *strcpy(char *dst , char const *src);
The strcpy function takes two string parameters and copies the src string to the dst parameter. When using the strcpy function, it is important to ensure that the target character array dst is long enough to hold the contents of the source character array src. If src is longer than dst, the remaining characters of src will still be copied, and they will overwrite the values in the memory space after dst; if the original memory space already has values, this will lead to data loss, which can cause serious consequences. To solve this problem, C language provides a safer way to perform string copying operations—the strncpy function:
char *strncpy(char *dst , char const *src , size_t len);
The strncpy function has three parameters, and like the strcpy function, it also copies characters from the src string to the target array dst, but the third parameter len specifies the number of characters that can be written to dst:
1) If strlen(src) > len, only len characters will be copied to dst, and dst will not be null-terminated (that is, the result of the strncpy call may not be a string);
2) If strlen(src) < len, all characters in src will be copied to dst, and the remaining part of dst will be filled with NUL.
4. Concatenating Strings
The library function strcat is used in C language to concatenate two strings:
char *strcat(char *dst,char const *src);
The strcat function appends the src string to the end of the dst string. Like the strcpy function, it is necessary to ensure that the remaining space in dst is sufficient to hold the entire src string. C language provides the strncat function to solve this problem:
char *strncat(char *dst , char const *src , size_t len);
The strncat function copies at most len characters from src to the end of the target array dst, and strncat always appends a NUL byte at the end of the resulting string, and unlike the strncpy function, it does not fill the remaining space in dst with NUL.
5. Comparing Strings
The library function strcmp is used in C language for string comparison. The strcmp function compares the two strings character by character until a mismatch is found: the string corresponding to the smaller character among the first mismatched characters is considered less than the other string; if all characters match, the two strings are considered equal;
int strcmp(char const *s1 , char const *s2);
The return values of this function are as follows:
1) s1 is less than s2, returns a negative value;
2) s1 is equal to s2, returns 0;
3) s1 is greater than s2, returns a positive value.
char *strncmp(char const *s1 , char const *s2 , size_t len);
The strncmp function can be used to limit the number of characters compared, with return values similar to strcmp, but only for the first len characters.
6. String Searching
6.1 Finding a Character
The strchr function or strrchr function can be used to find a specific character in a string:
char *strchr(char const *str,int ch); // int ch is the ASCII code of the character
char *strrchr(char const *str,int ch);
The strchr function searches for the first occurrence of character ch in the string str and returns a pointer to that position; if the character is not found, it returns a NULL pointer. The strrchr function searches for the last occurrence of character ch in the string and returns a pointer to that position.
6.2 Finding Any Characters
The strpbrk function can be used to find the first occurrence of any group of characters in a string:
char *strpbrk(char const *str , char const *group);
This function returns a pointer to the position of the first character in string str that matches any character in group; if no matches are found, it returns a NULL pointer.
6.3 Finding a Substring
The strstr function can be used to find a substring in a string:
char *strstr(char const *str1 , char const *str2);
This function searches for the first occurrence of the entire string str2 in str1 and returns a pointer to that position; if str2 does not fully appear in str1, the function returns a NULL pointer; if str2 is an empty string, it returns str1.
*Note: This article is compiled from the internet, and the copyright belongs to the original author. If there is any incorrect source information or infringement of rights, please contact us for deletion or authorization matters.
