首页 > 解决方案 > 如何从c中的段落中删除一个字符?

问题描述

我编写了 ac 程序,它从字符串中删除一个字符,而不是从多行中删除,然后我编写代码来存储字符串的多行,但是当我编译我的代码时,我无法获得正确的输出。请有人看一看。

#include <stdio.h>

int main()
{
    char inputString[1000], ch, outputString[1000];

    int i,j=0;

    printf ("Enter a multi line string:\n");
    scanf ("%[^;]s", inputString);
    gets(inputString);

    printf ("Enter a letter to be removed:\n");
    scanf ("%c", &ch);
    for (i=0; inputString[i]!='\0';i++)
    {
        if(inputString[i]==ch)
        {
            outputString[j]=inputString[i+1];
            i++;
        }
        else
        {
            outputString[j]=inputString[i];
        }
        j++;
    }
    for (i=0;i<j;i++)
    {
        printf ("%c", outputString[i]);
    }
    return 0;
}

标签: c

解决方案


你的程序有几个问题

1) 有

scanf ("%[^;]s", inputString);
gets(inputString);

你用scanf之后的get重写inputString,例如,如果输入是你设置inputString,那么你再次设置它,这样你就失去了scanf得到的所有东西foo bar loop;foo bar loop;

2)scanfgets都可以写出inputString,你没有任何保护,可以给scanf读取的最大大小,你可以用fgets替换gets的使用(忘记你在inputString中重写的事实)

3)你从不考虑EOF的情况,总是假设输入了一些东西

4进

    if(inputString[i]==ch)
    {
        outputString[j]=inputString[i+1];
        i++;
    }
    else
    {
        outputString[j]=inputString[i];
    }
    j++;

outputString[j]=inputString[i+1];假设您从来没有连续两次要删除的字符,那么这样做是错误的。

5)使用固定大小的数组不是强制性的,分配数组然后需要时重新分配

6) 在

for (i=0;i<j;i++)
{
    printf ("%c", outputString[i]);
}

最好以空字符结束outputString以仅通过fputs打印或puts添加最终的 \n 以防丢失


这里有一个建议:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  char * inputString = NULL;
  size_t sz = 0, used = 0;
  int v;

  printf ("Enter a multi line string (ended by ';'):\n");

  while ((v = getchar()) != ';') {
    if (v == EOF) {
      puts("premature EOF, abort");
      return -1;
    }

    if (used == sz) {
      sz += 100;
      inputString = realloc(inputString, sz);
      if (inputString == NULL) {
        puts("not enough memory");
        return -1;
      }
    }
    inputString[used++] = v;
  }

  /* read up to end of line */
  char * outputString = 0;

  if (getline(&outputString, &sz, stdin) == -1) {
    puts("premature EOF or not enough memory");
    return -1;
  }
  free(outputString);

  printf ("Enter a letter to be removed:\n");

  char ch;

  if (scanf ("%c", &ch) != 1) {
     puts("premature EOF, abort");
     return -1;
  }

  size_t i, j = 0;

  outputString = malloc(used + 1); /* worst case, +1 to have place for null character */

  for (i = 0; i != used; ++i)
  {
    if (inputString[i] != ch)
      outputString[j++] = inputString[i];
  }
  outputString[j] = 0;

  puts(outputString);

  free(inputString);
  free(outputString);

  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -g l.c
pi@raspberrypi:/tmp $ ./a.out
Enter a multi line string (ended by ';'):
this is a paragraph
  terminated by
a ;!!
Enter a letter to be removed:
i
ths s a paragraph
  termnated by
a 

在valgrind下执行:

pi@raspberrypi:/tmp $ valgrind ./a.out
==3900== Memcheck, a memory error detector
==3900== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3900== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==3900== Command: ./a.out
==3900== 
Enter a multi line string (ended by ';'):
this is a paragraph
  terminated by
a ;!!
Enter a letter to be removed:
i
ths s a paragraph
  termnated by
a 
==3900== 
==3900== HEAP SUMMARY:
==3900==     in use at exit: 0 bytes in 0 blocks
==3900==   total heap usage: 5 allocs, 5 frees, 2,307 bytes allocated
==3900== 
==3900== All heap blocks were freed -- no leaks are possible
==3900== 
==3900== For counts of detected and suppressed errors, rerun with: -v
==3900== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)

推荐阅读