从c中的字符串行中删除多余的字符

remove extra characters from a string line in c

我有一个作业要编写一个程序,该程序通过 getchar 将 2 个参数作为字符串并保存在变量中。第二个字符串定义了第一个字符串的长度,这意味着如果第一个字符串有 23 个字符而第二个字符串有 13 个,代码将打印第一个字符串的 13 个第一个字符并将 删除其余 。该问题包含一个代码,我必须完成缺少的部分。我写了我的代码,但我的程序在循环中给我输出,没有 for 循环它不能正常工作。

我不明白为什么它不起作用,所以如果有人能帮助我,我将非常感激和高兴。

输入:

字符串 1:this is a sample string(23 个字符)

字符串 2:sample length(13 个字符)

输出:

this is a sam(13 个字符)

原代码是这样的:

/*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] = '[=10=]';

    /* Discards any characters that remain on input line        */
    while (ch != '\n' && ch != EOF)
        ch = getchar();

    return (dest);
}

以下代码是我的,输出有问题,可能还有其他错误,但这是我最好的尝试:

#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] = '[=11=]';}
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] = '[=11=]';}
dest_len= strlen(dest);
strncpy(dest, temp, dest_len);
dest[dest_len]='[=11=]';

/* Discards any characters that remain on input line */
while (ch != '\n' && ch != EOF)
ch = getchar();
return (dest);
}

int main()
{
    char dest[BuffSize];
    char temp[BuffSize];
    int dest_len;
    printf("enter string\n");
    scanline(dest, dest_len);
    
    //my code has problem without the following loop
    for (int i=0; i<=dest_len; i++)
    {
        printf("%s", dest);
    }
    return 0;
}

我在代码中的问题是这部分

    for (int i=0; i<=dest_len; i++)
    {
        printf("%s", dest);
    }

所以基本上我将 dest_len 更改为 sizeof(dest) 作为 sizeof() returns 此处指针类型所需的大小,因此代码在不引用 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] = '[=11=]';}
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] = '[=11=]';}
dest_len= strlen(dest);
strncpy(dest, temp, dest_len);
dest[dest_len]='[=11=]';

/* 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 秒