繁体   English   中英

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

[英]remove extra characters from a string line in c

我有一个任务要编写一个程序,该程序将 2 arguments 作为字符串由getchar保存并保存在变量中。 第二个字符串定义了第一个字符串的长度,这意味着如果第一个字符串有 23 个字符,而第二个字符串有 13 个字符,则代码将打印第一个字符串的前 13 个字符删除 rest 该问题包含一个代码,我必须完成缺失的部分。 我写了我的代码,但是我的程序在一个循环中给了我 output 并且没有for循环它不能正常工作。

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

输入:

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

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

output

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] = '\0';

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

    return (dest);
}

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

#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];
    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);
    }

所以基本上我用sizeof(dest)更改了dest_len ,因为sizeof()返回指针类型在此处所需的大小,因此代码可以正常工作而无需引用未初始化的dest_len 这是没有错误并给出正确 output 的最终代码:

#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;
}

output:

输入字符串

这是一条示例线

输入第二个字符串

样本长度

这是山姆

进程返回 0 (0x0) 执行时间:11.890 s

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM