C中的字符串复制
2021-07-07 08:39:51
深夜i
--
--
C
中
的
字
符
串
复
制
使用库函数 strcpy 而不使用 strcpy 复制字符串的 C 程序。
字符串拷贝C程序
#include <stdio.h>
#include <string.h>
int main()
{
char source[1000], destination[1000];
printf("Input a string\n");
gets(source);
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
return 0;
}
程序输出:
在不使用 strcpy 的情况下在 C 中复制字符串
#include <stdio.h>
int main()
{
int c = 0;
char s[1000], d[1000] = "What can I say about my programming skills?";
printf("Before copying, the string: %s\n", d);
printf("Input a string to copy\n");
gets(s);
while (s[c] != '\0') {
d[c] = s[c];
c++;
}
d[c] = '\0';
printf("After copying, the string: %s\n", d);
return 0;
}
程序的输出:
Before copying, the string: What can I say about my programming skills?
Input a string to copy
My programming skills are improving.
After copying, the string: My programming skills are improving.
创建一个函数来复制一个字符串
#include <stdio.h>
void copy_string(char [], char []);
int main() {
char s[1000], d[1000];
printf("Input a string\n");
gets(s);
copy_string(d, s);
printf("The string: %s\n", d);
return 0;
}
void copy_string(char d[], char s[]) {
int c = 0;
while (s[c] != '\0') {
d[c] = s[c];
c++;
}
d[c] = '\0';
}
使用指针复制字符串的C程序
使用指针复制字符串的函数。
void copy_string(char *target, char *source) {
while (*source) {
*target = *source;
source++;
target++;
}
*target = '\0';
}
上一篇:
idea打包java可执行jar包
下一篇:
C中的字符串连接
评论区