简体   繁体   English

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

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

I had an assignment to write a program that takes 2 arguments as string by getchar and saves in variables.我有一个任务要编写一个程序,该程序将 2 arguments 作为字符串由getchar保存并保存在变量中。 The 2nd string defines the length of the 1st string, which means that if the first string has 23 characters and the 2nd one has 13, the code will print the 13 first characters of the 1st string and will remove the rest .第二个字符串定义了第一个字符串的长度,这意味着如果第一个字符串有 23 个字符,而第二个字符串有 13 个字符,则代码将打印第一个字符串的前 13 个字符删除 rest the question contained a code and I had to complete the missing parts.该问题包含一个代码,我必须完成缺失的部分。 I wrote my code but my program gives me the output in a loop and without the for loop it doesn't work properly.我写了我的代码,但是我的程序在一个循环中给了我 output 并且没有for循环它不能正常工作。

I couldn't understand why didn't it work so I would be very thankful and happy if anyone could help me.我不明白为什么它不起作用,所以如果有人可以帮助我,我将非常感激和高兴。

input:输入:

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

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

output : output

this is a sam (13 chars) this is a sam (13 个字符)

the original code was this:原始代码是这样的:

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

The following code is mine which has problem with the output and might have other bugs but it was my best try:以下代码是我的,它与 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;
}

my problem in code was this part我在代码中的问题是这部分

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

so basically I changed dest_len with sizeof(dest) as sizeof() returns the size required here by the pointer type, so the code worked properly without referring to dest_len which was not initialized.所以基本上我用sizeof(dest)更改了dest_len ,因为sizeof()返回指针类型在此处所需的大小,因此代码可以正常工作而无需引用未初始化的dest_len This is the final code that didn't have bug and gave the correct output:这是没有错误并给出正确 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: output:

enter string输入字符串

this is a sample line这是一条示例线

enter second string输入第二个字符串

sample length样本长度

this is a sam这是山姆

Process returned 0 (0x0) execution time: 11.890 s进程返回 0 (0x0) 执行时间:11.890 s

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

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