随便定义一个char类型字符串数组,此以char string[] = "iphone";为例。
实现strlen
char string[] = "iphone";
int i = 0;
while (string[i] != ‘\0‘) {
i ++;
}
printf("%d", i);
实现strcpy
char string1[20] = "ios";
char string2[] = "android";
//strcpy(string1,string2);
int i = 0;
while (string2[i] != ‘\0‘) { //计算机识别到字符串后面 ‘\0’,条件成立,拷贝停止
string1[i] = string2[i];
i ++;
}
string1[i] = ‘\0‘; //第i个字符串是‘\0’,最后给字符串加上
printf("%s", string1);
实现strcat
char string1[] = "ios";
char string2[] = "android";
long int length = strlen(string1);
int i = 0;
while (string2[i] != ‘\0‘) { //计算机直到识别到 ‘\0’,条件成立,拼接停止
string1[i + length] = string2[i];
i ++;
}
string1[i + length] = ‘\0‘; //加上目的字符串的长度
printf("%s", string1);
时间: 2024-10-13 03:09:37