[英]Implementing strcpy function without using in-built strcpy
编辑:问题已解决,我非常感谢所有帮助过我的人,如果您将来看到这篇文章,这里是在 C 中实现 strcpy函数的工作代码:
#include <stdio.h>
void strcopy(char *a, char *b)
{
for (int i = 0; a[i] != '\0'; i++)
{
b[i] = a[i];
if(a[i+1] == '\0')
b[i+1]='\0';
}
}
int main()
{
printf("Enter the string: ");
char a[10];
scanf("%s",&a);
printf("Input string: %s", a);
char b[sizeof(a)];
strcopy(a,b);
printf("\nOutput string: %s", b);
return 0;
}
输出:
Enter the string: helloworld Input string: helloworld Output string: helloworld
问题从这里开始:
我正在尝试在不使用内置函数的情况下实现 strcpy 函数。 我在网上搜索过,但我只找到了执行此任务的程序,但没有找到功能。
我尝试了各种方法,但没有运气。 最后(尝试 4),我写了一个有效的代码,但我不明白为什么这东西有效,但以前的方法却没有
只是寻找一个函数,它可以在从 main() 调用时将一个字符串的值复制到另一个字符串中,并在输出字符串中显示正确的值。 如果我能得到它,我将非常感激。 当然,我不能使用内置函数,因为我被迫在星期一进行考试,因为我们必须实现随机 7 个字符串函数中的任何一个并解释代码。
编辑:还发现这个https://www.programmersought.com/article/29163684184/代码来实现同样的事情,但它是用 C++ 编写的。 为什么在 C++ 发明之后学校/学院仍然教授 C? 任何人都知道任何 C++ 到 C 转换器?
尝试 1:
#include <stdio.h>
void strcopy(char *a, char *b)
{
for (int i = 0; a[i] != '\0'; i++)
{
b[i] = a[i];
}
}
int main()
{
char a[10];
printf("Enter string: ");
scanf("%s", &a);
printf("Input string: %s", a);
char b[(sizeof(a))];
strcopy(a,b);
printf("\nOutput string: %s", b);
return 0;
}
输出:
Enter string: helloworld Input string: helloworld Output string: helloworldhelloworld
尝试2:
#include <stdio.h>
void strcopy(char *a, char *b)
{
for (int i = 0; a[i] != '\0'; i++)
{
b[i] = a[i];
}
}
int main()
{
int n;
printf("Enter the string size: ");
scanf("%d",&n);
char a[n];
printf("Enter string: ");
scanf(" %s",&a);
printf("Input string: %s", a);
char b[n];
strcopy(a,b);
printf("\nOutput string: %s", b);
return 0;
}
输出:
Enter the string size: 7 Enter string: helloworld Input string: helloworld Output string: helloworldö
尝试3:
#include <stdio.h>
void strcopy(char *a, char *b)
{
for (int i = 0; a[i] != '\0'; i++)
{
b[i] = a[i];
}
}
int main()
{
int n;
printf("Enter the string size: ");
scanf("%d",&n);
n++;
char a[n];
printf("Enter string: ");
// scanf(" %s",&a);
fgets(a,n,stdin);
fgets(a,n,stdin); //need to write twice or it wont work
printf("Input string: %s", a);
char b[n];
strcopy(a,b);
printf("\nOutput string: %s", b);
return 0;
}
输出:
Enter the string size: 5 Enter string: helloworld Input string: hello Output string: hello⌂
尝试4:
#include <stdio.h>
void strcopy(char *a, char *b)
{
for (int i = 0; a[i] != '\0'; i++)
{
b[i] = a[i];
}
}
int main()
{
int n;
printf("Enter the string: ");
scanf("%d",&n);
n++;
char a[n];
scanf(" %s",&a);
// printf("Enter string: ");
// fgets(a,n,stdin);
// fgets(a,n,stdin);
printf("Input string: %s", a);
char b[n];
strcopy(a,b);
printf("\nOutput string: %s", b);
return 0;
}
输出:
Enter the string: helloworld Input string: helloworld Output string: helloworld
编辑:
朋友在回答中给出的尝试方法:
代码:
#include <stdio.h>
void strcopy(char *a, char *b)
{
for (int i = 0; a[i+1] != '\0'; i++)
{
b[i] = a[i];
}
}
int main()
{
// int n;
printf("Enter the string: ");
// scanf("%d",&n);
// n++;
char a[10];
scanf("%s",&a);
// printf("Enter string: ");
// fgets(a,n,stdin);
// fgets(a,n,stdin);
printf("Input string: %s", a);
char b[sizeof(a)];
strcopy(a,b);
printf("\nOutput string: %s", b);
return 0;
}
输出:
Enter the string: helloworld Input string: helloworld Output string: helloworl
编辑2:
尝试 do-while 循环:
代码:
#include <stdio.h>
void strcopy(char *a, char *b)
{
// for (int i = 0; a[i+1] != '\0'; i++)
// {
// b[i] = a[i];
// }
int i=0;
do{
b[i]=a[i];
i++;
}while(a[i] != '\0');
}
int main()
{
// int n;
printf("Enter the string: ");
// scanf("%d",&n);
// n++;
char a[10];
scanf("%s",&a);
// printf("Enter string: ");
// fgets(a,n,stdin);
// fgets(a,n,stdin);
printf("Input string: %s", a);
char b[sizeof(a)];
strcopy(a,b);
printf("\nOutput string: %s", b);
return 0;
}
输出:
输入字符串:helloworld 输入字符串:helloworld 输出字符串:helloworldhelloworld
OP 的strcopy()
在尝试模仿标准库函数时存在各种问题:
char *strcpy(char * restrict s1, const char * restrict s2);
目标数组中缺少空字符
这是 OP 的主要问题。 必须以某种方式在目标中分配一个空字符。 OP 的各种代码都没有做到这一点。
提示:考虑复制一个字符串""
,它只为目标分配一个空字符'\\0'
。
错误的参数顺序
第一个参数应该是匹配strcpy()
的目标
缺少返回值
strcpy()
返回原始目标指针。
高级:参数缺少restrict/const
属性
只需使用上面的库函数签名。
高级:长字符串失败
OP 的代码使用int
来索引这对于长字符串是不够的。 最好使用size_t
。
提示:添加输出清晰度
打印结果时,添加标记字符,如< >
和最后的'\\n'
。
对于调试工作,将目标字符串设为 2 倍大。
// char b[sizeof(a)];
char b[sizeof(a) *2];
strcopy(a,b);
// printf("\nOutput string: %s", b);
printf("\nOutput string: <%s>\n", b);
OP 在不是家庭作业编码任务中断言这一点,因此候选解决方案:
char *strcpy(char * restrict s1, const char * restrict s2) {
// Use unsigned char as all str functions behave as if char was unsigned
// Also need to save s1 for return
unsigned char *us1 = (unsigned char *)s1;
const unsigned char *us2 = (const unsigned char *)s2;
while (*us2) {
*us1++ = *us2++;
}
return s1;
}
在 K&R 为王的过去,我们会在腰带上系一个洋葱,就像当时的风格一样,并做一些类似的事情
void strcopy(char *a, char *b)
{
while ( *b++ = *a++ ) {}
}
现在我认为旧的方式已经贬值了
有人建议我解释为什么会这样。
*b = *a
做复制一个字节的核心功能
*b++ = *a++
添加自动增量,在赋值完成后将两个指针移动到内存中的下一个位置
while (*b++ = *a++) {}
循环,而复制的字节不为零。 空的{}
只是为了完成while
语句的语法
因此,while 循环将运行复制字节,然后将两个指针递增到下一个位置,直到复制的字节为零,即字符串的结尾
您没有添加终止\\0
。
你所有的实现都有相同的基本错误; “try 4”仅在偶然情况下起作用。 问题在于strcopy
的循环终止条件。 当您到达结束源字符串的 NUL 字符时,您将停止循环而不是复制它。 这意味着目标字符串没有结束并且printf
越过它的末尾,打印任何发生在内存中的内容。 您必须改为复制 NUL ,然后停止循环。
您在如何为字符串a
和b
分配内存方面也有错误——在每个实现中都不同,但在我看来它们都没有抽象正确。 与您的讲师讨论如何正确执行此操作,这是 C 中最复杂的主题之一,我没有足够的信息说明您可以使用哪些技术。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.