删除c中字符串行中的额外字符

删除c中字符串行中的额外字符,c,string,getchar,C,String,Getchar,我的任务是编写一个程序,通过getchar将两个参数作为字符串,并保存在变量中。第二个字符串定义了第一个字符串的长度,这意味着如果第一个字符串有23个字符,第二个字符串有13个字符,代码将打印第一个字符串的13个前字符,并将删除其余的。这个问题包含一个代码,我必须完成缺少的部分。我写了我的代码,但是我的程序在循环中给我输出,如果没有for循环,它就不能正常工作 我不明白为什么它不起作用,所以如果有人能帮助我,我会非常感激和高兴 输入: /*Gets one line of data from s

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

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

输入:

/*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);
}

#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;
}
字符串1:
这是一个示例字符串
(23个字符)

字符串2:
样本长度
(13个字符)

输出

这是一个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);
}

#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;
}
/*从标准输入中获取一行数据。返回一个空字符串
文件结束。如果数据线无法容纳分配的空间,则存储
适合并丢弃输入行其余部分的部分*/
煤焦*
扫描线(字符*dest,/*输出-目标字符串*/
int dest_len)/*输入-dest中的可用空间*/
{
int i,ch;
/*每次获取下一行一个字符*/
i=0;
for(ch=getchar();
ch!='\n'和&ch!=EOF和&i
以下代码是我的,它的输出有问题,可能有其他错误,但这是我最好的尝试:

/*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);
}

#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;
}
#包括
#包括
#定义大小256
char*scanline(char*dest,int dest_len)/*输出-目标字符串*//*输入-目标中可用的空间*/
{
int i,ch;
字符温度[BuffSize];
//获取第一个字符串并存储在temp中
i=0;
对于(ch=getchar();ch!='\n'&&ch!=EOF&&i对于(inti=0;i我在代码中的问题是这一部分

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

/*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);
}

#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;
}
输入字符串

这是一条采样线

输入第二个字符串

样本长度

这是山姆


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

我注意到的一件事是,
dest_len
从未在
main
中初始化。您从未在
scanline
函数中使用它,因此它最终并不重要,但这意味着您无法防止写入超过
dest
所能容纳的字符数。但是回到
main
中,您会发现p从0到
dest_len
,这是未初始化的。你应该帮自己一个忙,正确设置代码的格式。即使对我们来说,处理格式不好的代码也很困难,更不用说对你了。好吧,在你发布的原始
scanline
函数中,
dest_len
充当了一个限制,以防止超过该数量的字符读入
dest
,即防止溢出。如果希望
dest\u len
是包含写入
dest
的字符串实际长度的输出,则需要将其声明为
int*
,以便将值返回给调用方(即,它是一个输出参数)。但它仍然无法解决这样一个问题:您没有以任何方式限制写入
dest
,并且仍然会出现溢出。还要注意,在
main
中的循环中执行
printf
没有意义。
printf(“%s”,dest);
已经打印了
dest
中包含的整个字符串(假设它正确地以null结尾)因此根本不需要循环,只需要一条打印语句。在ISO C中,如果没有字符串大小的限制,这实际上是很难做到的;我当然不会将它提供给初学者。也许可以问一下,您能确定不会超过的合理大小是多少?