C語言是一種廣泛使用的編程語言,尤其在系統編程和嵌入式開發中占有重要地位。字符串操作是C語言編程中的一個重要部分,掌握字符串的各種操作方法對于編寫高效、可靠的程序至關重要。本文將詳細介紹C語言中常見的字符串操作方法,幫助讀者更好地理解和應用這些技術。
在C語言中,字符串是由字符組成的數組,以空字符'\0'
結尾。字符串的每個字符都是一個char
類型的變量,存儲在連續的內存地址中。由于字符串以'\0'
結尾,因此可以通過遍歷字符數組直到遇到'\0'
來確定字符串的長度。
在C語言中,字符串可以通過多種方式聲明和初始化。
char str[] = "Hello, World!";
這種方式會在編譯時自動在字符串末尾添加'\0'
。
char *str = "Hello, World!";
這種方式將字符串常量存儲在只讀內存中,指針str
指向該字符串的首地址。
char str[14] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
這種方式需要手動添加'\0'
。
printf
和scanf
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
scanf
函數會讀取輸入直到遇到空白字符(空格、制表符、換行符等),并將其存儲在str
中。
gets
和puts
char str[100];
printf("Enter a string: ");
gets(str);
printf("You entered: ");
puts(str);
gets
函數會讀取整行輸入,直到遇到換行符,并將其存儲在str
中。puts
函數用于輸出字符串并自動添加換行符。
fgets
和fputs
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("You entered: ");
fputs(str, stdout);
fgets
函數會讀取指定數量的字符或直到遇到換行符,并將其存儲在str
中。fputs
函數用于輸出字符串,不會自動添加換行符。
strlen
函數#include <string.h>
char str[] = "Hello, World!";
int length = strlen(str);
printf("Length of the string: %d\n", length);
strlen
函數返回字符串的長度,不包括'\0'
。
char str[] = "Hello, World!";
int length = 0;
while (str[length] != '\0') {
length++;
}
printf("Length of the string: %d\n", length);
通過遍歷字符數組直到遇到'\0'
,可以手動計算字符串的長度。
strcpy
函數#include <string.h>
char src[] = "Hello, World!";
char dest[50];
strcpy(dest, src);
printf("Copied string: %s\n", dest);
strcpy
函數將src
字符串復制到dest
中,包括'\0'
。
strncpy
函數#include <string.h>
char src[] = "Hello, World!";
char dest[50];
strncpy(dest, src, 5);
dest[5] = '\0';
printf("Copied string: %s\n", dest);
strncpy
函數將src
字符串的前n
個字符復制到dest
中,不會自動添加'\0'
,因此需要手動添加。
char src[] = "Hello, World!";
char dest[50];
int i = 0;
while (src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
printf("Copied string: %s\n", dest);
通過遍歷src
字符串并逐個字符復制到dest
中,可以手動實現字符串復制。
strcat
函數#include <string.h>
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
strcat
函數將str2
字符串連接到str1
的末尾,包括'\0'
。
strncat
函數#include <string.h>
char str1[50] = "Hello, ";
char str2[] = "World!";
strncat(str1, str2, 3);
printf("Concatenated string: %s\n", str1);
strncat
函數將str2
字符串的前n
個字符連接到str1
的末尾,并自動添加'\0'
。
char str1[50] = "Hello, ";
char str2[] = "World!";
int i = 0, j = 0;
while (str1[i] != '\0') {
i++;
}
while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
printf("Concatenated string: %s\n", str1);
通過遍歷str1
找到末尾,然后將str2
的字符逐個復制到str1
的末尾,可以手動實現字符串連接。
strcmp
函數#include <string.h>
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
strcmp
函數比較兩個字符串,返回值為0表示相等,小于0表示str1
小于str2
,大于0表示str1
大于str2
。
strncmp
函數#include <string.h>
char str1[] = "Hello";
char str2[] = "World";
int result = strncmp(str1, str2, 3);
if (result == 0) {
printf("First 3 characters are equal\n");
} else if (result < 0) {
printf("First 3 characters of str1 are less than str2\n");
} else {
printf("First 3 characters of str1 are greater than str2\n");
}
strncmp
函數比較兩個字符串的前n
個字符,返回值與strcmp
相同。
char str1[] = "Hello";
char str2[] = "World";
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
break;
}
i++;
}
if (str1[i] == '\0' && str2[i] == '\0') {
printf("Strings are equal\n");
} else if (str1[i] < str2[i]) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
通過逐個字符比較兩個字符串,可以手動實現字符串比較。
strchr
函數#include <string.h>
char str[] = "Hello, World!";
char *result = strchr(str, 'o');
if (result != NULL) {
printf("Found 'o' at position: %ld\n", result - str);
} else {
printf("'o' not found\n");
}
strchr
函數在字符串中查找指定字符的第一個出現位置,返回指向該位置的指針,如果未找到則返回NULL
。
strrchr
函數#include <string.h>
char str[] = "Hello, World!";
char *result = strrchr(str, 'o');
if (result != NULL) {
printf("Found last 'o' at position: %ld\n", result - str);
} else {
printf("'o' not found\n");
}
strrchr
函數在字符串中查找指定字符的最后一個出現位置,返回指向該位置的指針,如果未找到則返回NULL
。
strstr
函數#include <string.h>
char str[] = "Hello, World!";
char *result = strstr(str, "World");
if (result != NULL) {
printf("Found 'World' at position: %ld\n", result - str);
} else {
printf("'World' not found\n");
}
strstr
函數在字符串中查找指定子字符串的第一個出現位置,返回指向該位置的指針,如果未找到則返回NULL
。
char str[] = "Hello, World!";
char target = 'o';
int i = 0;
while (str[i] != '\0') {
if (str[i] == target) {
printf("Found '%c' at position: %d\n", target, i);
break;
}
i++;
}
if (str[i] == '\0') {
printf("'%c' not found\n", target);
}
通過遍歷字符串并逐個字符比較,可以手動實現字符查找。
strncpy
函數#include <string.h>
char str[] = "Hello, World!";
char substr[10];
strncpy(substr, str + 7, 5);
substr[5] = '\0';
printf("Substring: %s\n", substr);
strncpy
函數可以從指定位置開始截取指定長度的子字符串。
char str[] = "Hello, World!";
char substr[10];
int start = 7, length = 5;
int i = 0;
while (i < length) {
substr[i] = str[start + i];
i++;
}
substr[i] = '\0';
printf("Substring: %s\n", substr);
通過從指定位置開始復制指定長度的字符,可以手動實現字符串截取。
strstr
和strncpy
函數#include <string.h>
char str[] = "Hello, World!";
char old[] = "World";
char new[] = "C";
char result[50];
char *pos = strstr(str, old);
if (pos != NULL) {
strncpy(result, str, pos - str);
result[pos - str] = '\0';
strcat(result, new);
strcat(result, pos + strlen(old));
printf("Result: %s\n", result);
} else {
printf("Substring not found\n");
}
通過查找子字符串的位置,然后使用strncpy
和strcat
函數進行替換。
char str[] = "Hello, World!";
char old[] = "World";
char new[] = "C";
char result[50];
int i = 0, j = 0;
while (str[i] != '\0') {
if (strstr(&str[i], old) == &str[i]) {
strcpy(&result[j], new);
i += strlen(old);
j += strlen(new);
} else {
result[j] = str[i];
i++;
j++;
}
}
result[j] = '\0';
printf("Result: %s\n", result);
通過遍歷字符串并逐個字符比較,可以手動實現字符串替換。
toupper
和tolower
函數#include <ctype.h>
char str[] = "Hello, World!";
int i = 0;
while (str[i] != '\0') {
str[i] = toupper(str[i]);
i++;
}
printf("Uppercase string: %s\n", str);
i = 0;
while (str[i] != '\0') {
str[i] = tolower(str[i]);
i++;
}
printf("Lowercase string: %s\n", str);
toupper
函數將字符轉換為大寫,tolower
函數將字符轉換為小寫。
char str[] = "Hello, World!";
int i = 0;
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
i++;
}
printf("Uppercase string: %s\n", str);
i = 0;
while (str[i] != '\0') {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] + 32;
}
i++;
}
printf("Lowercase string: %s\n", str);
通過判斷字符的ASCII值并進行加減操作,可以手動實現字符串的大小寫轉換。
strtok
函數#include <string.h>
char str[] = "Hello, World!";
char *token = strtok(str, " ,!");
while (token != NULL) {
printf("Token: %s\n", token);
token = strtok(NULL, " ,!");
}
strtok
函數根據指定的分隔符將字符串分割成多個子字符串,每次調用返回一個子字符串。
char str[] = "Hello, World!";
char delimiter = ' ';
char *start = str;
char *end = str;
while (*end != '\0') {
if (*end == delimiter) {
*end = '\0';
printf("Token: %s\n", start);
start = end + 1;
}
end++;
}
printf("Token: %s\n", start);
通過遍歷字符串并根據分隔符進行分割,可以手動實現字符串分割。
sprintf
函數#include <stdio.h>
char str[50];
int num = 123;
sprintf(str, "The number is %d", num);
printf("Formatted string: %s\n", str);
sprintf
函數將格式化的數據寫入字符串中。
snprintf
函數#include <stdio.h>
char str[50];
int num = 123;
snprintf(str, sizeof(str), "The number is %d", num);
printf("Formatted string: %s\n", str);
snprintf
函數將格式化的數據寫入字符串中,并限制寫入的字符數量。
char str[50];
int num = 123;
int i = 0;
char *format = "The number is %d";
while (format[i] != '\0') {
str[i] = format[i];
i++;
}
sprintf(&str[i], "%d", num);
printf("Formatted string: %s\n", str);
通過手動拼接字符串和格式化數據,可以手動實現字符串格式化。
memcpy
函數#include <string.h>
char src[] = "Hello, World!";
char dest[50];
memcpy(dest, src, strlen(src) + 1);
printf("Copied string: %s\n", dest);
memcpy
函數將指定數量的字節從源內存區域復制到目標內存區域。
memset
函數#include <string.h>
char str[50];
memset(str, 'A', 49);
str[49] = '\0';
printf("Filled string: %s\n", str);
memset
函數將指定數量的字節設置為指定值。
memcmp
函數#include <string.h>
char str1[] = "Hello";
char str2[] = "World";
int result = memcmp(str1, str2, 5);
if (result == 0) {
printf("First 5 bytes are equal\n");
} else if (result < 0) {
printf("First 5 bytes of str1 are less than str2\n");
} else {
printf("First 5 bytes of str1 are greater than str2\n");
}
memcmp
函數比較兩個內存區域的前n
個字節,返回值與strcmp
相同。
memmove
函數#include <string.h>
char str[] = "Hello, World!";
memmove(str + 7, str, 5);
printf("Moved string: %s\n", str);
memmove
函數將指定數量的字節從源內存區域移動到目標內存區域,即使源
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。