您好,登錄后才能下訂單哦!
在C語言中,字符串是一系列字符的集合,以空字符(‘\0’)結尾
#include<stdio.h>
int main() {
char str[] = "Hello, World!";
int i;
for (i = 0; str[i] != '\0'; i++) {
printf("%c", str[i]);
}
return 0;
}
#include<stdio.h>
int string_length(const char *str) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
char str[] = "Hello, World!";
int length = string_length(str);
printf("Length of the string: %d\n", length);
return 0;
}
#include<stdio.h>
#include<string.h>
int main() {
char str[] = "Hello, World!";
char substr[] = "World";
int found = 0;
for (int i = 0; i <= strlen(str) - strlen(substr); i++) {
if (strncmp(&str[i], substr, strlen(substr)) == 0) {
found = 1;
break;
}
}
if (found) {
printf("Substring found.\n");
} else {
printf("Substring not found.\n");
}
return 0;
}
#include<stdio.h>
void string_concat(char *dest, const char *src) {
int dest_length = 0;
while (dest[dest_length] != '\0') {
dest_length++;
}
for (int i = 0; src[i] != '\0'; i++) {
dest[dest_length + i] = src[i];
}
dest[dest_length + strlen(src)] = '\0';
}
int main() {
char str1[] = "Hello, ";
char str2[] = "World!";
string_concat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
#include<stdio.h>
void string_copy(char *dest, const char *src) {
int i;
for (i = 0; src[i] != '\0'; i++) {
dest[i] = src[i];
}
dest[i] = '\0';
}
int main() {
char str1[] = "Hello, World!";
char str2[20];
string_copy(str2, str1);
printf("Copied string: %s\n", str2);
return 0;
}
#include<stdio.h>
#include <ctype.h>
void to_uppercase(char *str) {
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
}
int main() {
char str[] = "Hello, World!";
to_uppercase(str);
printf("Uppercase string: %s\n", str);
return 0;
}
這些技術可以幫助您在C語言中處理和分析字符串。根據需要,您可以組合這些技術來實現更復雜的字符串操作。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。