问题描述
我有一项任务是编写一个程序,该程序将 2 个参数作为字符串 structure(list(sample_a = c("AD001","AD001","AD002","AD003","AD004","AD005","AD006","AD007","AD008","AD009","AD010","AD011","AD012","AD013","AD014","AD015","AD016","AD017","AD018","AD019","AD020","AD021","AD022","AD023","AD024","AD025","AD026","AD027"),sample_b = c("AD002","AD027","AD028","AD028"),rel = c(-0.03991,-0.0249,-0.01788,-0.02618,-0.003831,-0.0003193,0.00447,-0.03768,-0.02554,-0.03512,-0.05268,-0.01948,-0.3177,-0.01692,0.004151,-0.03857,-0.008621,-0.00447,-0.02778,-0.009898,-0.2722,0.01054,0.002235,-0.03303,0.01615,0.01119,0.00713,-0.005846,-0.012,-0.01108,-0.001863,-0.02334,0.00315,-0.01785,-0.02194,-0.002775,-0.01669,-0.2779,-0.01021,-0.02185,-0.04339,-0.05598,-0.02432,-0.002786,-0.01186,-0.02276,-0.2833,-0.02246,0.009972,0.01122,0.009037,0.01932,0.01215,-0.001246,0.02586,0.6049,0.007874,0.0162,-0.01974,-0.005921,0.002493,-0.3015,-0.008293,0.002805,0.001286,-0.01518,0.005609,0.01652,-0.002809,-0.01714,-0.2803,0.03584,0.004051,0.01927,0.02889,0.03604,0.0205,0.01118,0.02641,0.0165,-0.005984,0.03573,-0.00282,0.00497,0.001553,-0.2583,0.005742,0.0174,0.002893,-0.01233,0.009009,0.02578,0.009363,-0.005299,-0.252,0.01771,0.02081,-0.01243,0.0202,0.01088,0.0003108,-0.01336,-0.007781,-0.01449,0.02735,-0.02068,-0.003729,0.01243,-0.3045,-0.03254,-0.01274,-0.03247,-0.01297,-0.009323,-0.001554,-0.006554,-0.01465,-0.3005,0.002797,-0.0006215,0.01457,0.03007,0.02821,0.02732,0.01587,0.009764,0.02728,0.01661,0.01209,-0.2398,-0.01372,0.02108,-0.01093,-0.01107,0.02449,0.02294,0.008427,-0.007481,-0.2665,0.04495,0.03348,0.03673,0.04471,0.01961,0.01134,0.03367,0.02946,0.008017,0.01793,-0.2552,0.0236,0.003061,-0.004179,0.01139,0.0157,0.02477,-0.007803,0.001247,-0.239,0.03796,0.02173,0.02416,0.04905,0.01836,-0.004409,0.04178,0.02131,0.0222,0.009583,-0.01157,0.02424,0.01592,-0.009965,0.0003163,-0.008929,0.0192,0.0231,0.01091,0.01565,0.02958,0.03478,0.02049,0.02521,-0.008819,0.02875,0.01254,0.02097,0.01144,-0.2556,-0.007337,0.03061,-0.006107,0.03226,-0.002771,0.01579,-0.009988,0.01496,-0.2711,0.0315,0.01621,0.02428,0.01953,0.03663,0.01504,0.008382,-0.001552,-0.2791,0.007337,0.007451,-0.008679,-0.02309,0.0326,0.02391,0.01029,-0.2375,0.02763,0.04222,0.004094,0.02583,-0.004701,-0.01961,-0.2737,0.0118,0.01369,-0.007715,-0.006958,0.007781,0.0103,-0.006234,-0.2635,0.03579,0.02148,0.01764,-0.008189,-0.01134,-0.01858,-0.3096,-0.001595,-0.004724,-0.02732,-0.02846,-0.02016,-0.001575,-0.02047,-0.3032,0.01039,0.008504,0.03855,0.02189,-0.2467,0.01148,0.02296,0.01382,0.006009,0.03158,0.03246,0.007481,-0.2593,0.04971,0.0421,-0.03071,-0.01128,0.005423,0.02382,-0.018,-0.04175,-0.004074,0.006894,-0.01316,0.01943,0.002194,-0.01051,-0.2868,-0.000638,-0.01418,-0.02411,-0.01992,0.004009,0.01424,0.006866,-0.03148,-0.2913,0.0296,0.01048,-0.2934,-0.01276,-0.01254,-0.004744,0.003091,-0.008739,-0.008416,-0.2787,0.02566,0.01855,-0.2587,-0.2537,-0.3115,-0.2772,-0.3053,-0.3165,-0.2945,0.01157,-0.2695,-0.2325,0.01021,-0.03118,-0.02392,0.000638,0.01085,0.01818,-0.02073,0.01786,0.01212,-0.02347,0.006325,0.002463,0.02539,-0.002185,-0.0134,-0.2417,0.02694,0.008877,-0.02829,-0.02861,-0.03439,-0.03568,-0.02668,-0.3028,0.004822,0.006429,0.01265,0.01107,-0.009171,-0.01044,-0.2738,0.009171,0.01581,0.006811,-0.01155,-0.004364,0.03633,0.0314,0.009051,0.003117,0.02415,0.04211,-0.0362,-0.3043,0.0009363,0.02341,0.02525,0.01808,-0.2482,-0.2497,0.05003)),row.names = c(NA,-378L),class = "data.frame")
并保存在变量中。第二个字符串定义了第一个字符串的长度,这意味着如果第一个字符串有 23 个字符,第二个字符串有 13 个,则代码将打印 第一个字符串的第 13 个字符,并将 删除其余的。该问题包含一个代码,我必须完成缺失的部分。我编写了代码,但我的程序在循环中给了我输出,如果没有 getchar
循环,它就无法正常工作。
我不明白为什么它不起作用,所以如果有人能帮助我,我会非常感激和高兴。
输入:
字符串 1:for
(23 个字符)
字符串 2:this is a sample string
(13 个字符)
输出:
sample length
(13 个字符)
原来的代码是这样的:
this is a sam
以下代码是我的,输出有问题,可能还有其他错误,但这是我最好的尝试:
/*Gets one line of data from standard input. Returns an empty string on
end of file. If data line will not fit in allotted space,stores
portion that does fit and discards rest of input line.*/
char *
scanline(char *dest,/* output - destination string */
int dest_len) /* input - space available in dest */
{
int i,ch;
/* Gets next line one character at a time. */
i = 0;
for (ch = getchar();
ch != '\n' && ch != EOF && i < dest_len - 1;
ch = getchar())
dest[i++] = ch;
dest[i] = '\0';
/* Discards any characters that remain on input line */
while (ch != '\n' && ch != EOF)
ch = getchar();
return (dest);
}
解决方法
我的代码问题是这部分
for (int i=0; i<=dest_len; i++)
{
printf("%s",dest);
}
所以基本上我用 dest_len
更改了 sizeof(dest)
因为 sizeof()
返回指针类型所需的大小,因此代码正常工作而没有引用 dest_len
初始化。这是没有错误并给出正确输出的最终代码:
#include <stdio.h>
#include <string.h>
#define BuffSize 256
char *scanline(char *dest,int dest_len)/* output - destination string */ /* input - space available in dest */
{
int i,ch;
char temp[BuffSize];
//get the first string and store in temp
i = 0;
for (ch = getchar(); ch != '\n' && ch != EOF && i < BuffSize; ch = getchar())
{temp[i++] = ch;
temp[i] = '\0';}
int len= strlen(temp);
printf("enter second string\n");
/* Gets next line one character at a time. */
i = 0;
int cha;
for (cha = getchar(); cha != '\n' && cha != EOF && i < dest_len - 1; cha = getchar())
{dest[i++] = cha;
dest[i] = '\0';}
dest_len= strlen(dest);
strncpy(dest,temp,dest_len);
dest[dest_len]='\0';
/* Discards any characters that remain on input line */
while (ch != '\n' && ch != EOF)
ch = getchar();
return (dest);
}
int main()
{
char dest[BuffSize];
printf("enter string\n");
printf("%s",scanline(dest,sizeof(dest)));
return 0;
}
输出:
输入字符串
这是一个示例线
输入第二个字符串
样本长度
这是一个山姆
进程返回 0 (0x0) 执行时间:11.890 s