[英]how to remove a character from a paragraph in 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;
}
您的程序中有幾個問題
1)與
scanf ("%[^;]s", inputString);
gets(inputString);
例如,如果輸入是foo bar loop;
則用scanf之后的gets重寫inputString foo bar loop;
您可以通過foo bar loop
設置inputString ,然后使用再次設置它;
所以你失去了scanf的全部
2) scanf和gets都可以從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)要使用固定大小的陣列不是強制性的,分配陣列然后在需要時的realloc
6)在
for (i=0;i<j;i++)
{
printf ("%c", outputString[i]);
}
更好的結束outputString以空字符,只是通過的fputs打印或提出添加最后\\ 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.