简体   繁体   English

C从字符串中删除字符

[英]C delete chars from string

I have program that asks to enter a string (mystring) and a char (ch). 我有要求输入字符串(mystring)和字符(ch)的程序。 Then it deletes all entered chars (ch) from the string (mystring). 然后,它从字符串(mystring)中删除所有输入的字符(ch)。 For example "abcabc" and char 'a' then the result shoud be "bcbc". 例如“ abcabc”和字符“ a”,则结果应为“ bcbc”。 -When I use scanf the program works nicely if the string does not have spaces. -当我使用scanf时,如果字符串没有空格,则程序运行良好。 If I enter "abc abc abc" It reads and processes only the first 3 letters (until space). 如果我输入“ abc abc abc”,它将仅读取和处理前3个字母(直到空格)。 Then I was advised to use gets(mystr); 然后建议我使用gets(mystr); because it can read all the stirng. 因为它可以读取所有搅拌器 But when I use gets the result is the same as the input string and nothing happens. 但是当我使用gets时,结果与输入字符串相同,什么也没发生。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100

int main(int argc, char *argv[])
{
   char mystr[N] ,result[N];
   char ch;
   int i,k;
   k=0;

   printf("enter string \n");   
   //gets(mystr);///////////////////////////
   //scanf("%s",&mystr);///////////////////
   printf("enter char \n");  
     scanf("%c",&ch);
     scanf("%c",&ch);

  for ( i = 0; i <= strlen(mystr); i++ )
  {
    if (mystr[i] != ch)
    {
      result[k]=mystr[i];
      k++;
    } 
  }
   puts(result);
   system("pause");
   return 0;
}
 scanf("%c",&ch);
 scanf("%c",&ch);

That second scanf is your problem. 第二个scanf是您的问题。 It's picking up the new-line character that you enter after the letter you want to remove (and overwrites the previous value of ch ). 它拾取您要删除的字母之后输入的换行符(并覆盖ch的先前值)。

Get rid of it. 摆脱它。

Please note, as the man page says: 请注意,如手册页所述:

Never use gets() . 永远不要使用gets() Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. 因为无法不事先知道数据就无法知道将读取多少个字符gets(),并且因为gets()将继续存储超过缓冲区末尾的字符,所以使用它非常危险。 It has been used to break computer security. 它已被用来破坏计算机的安全性。 Use fgets() instead. 使用fgets()代替。

hmm - not sure what the problem is - use getstr, but not scanf for the string, and it works for me in visual studio 嗯-不知道是什么问题-使用getstr,但不对字符串使用scanf,它在Visual Studio中对我有用

int main(int argc, char *argv[])
{
   char mystr[N] ,result[N];
   char ch;
   int i,k;
   k=0;

   printf("enter string \n");   
   gets(mystr);///////////////////////////
   //scanf("%s",&mystr);///////////////////
   printf("enter char \n");  
     scanf("%c",&ch);
    // scanf("%c",&ch);

  for ( i = 0; i <= strlen(mystr); i++ )
  {
    if (mystr[i] != ch)
    {
      result[k]=mystr[i];
      k++;
    } 
  }
   puts(result);
   system("pause");
   return 0;
}

Use this one: 使用这个:

char temp[2];
scanf("%1s",temp);
ch = temp[0];

and use gets 和使用gets

scanf when used with char s has some problems (it gets the "old" new line). char s一起使用时, scanf有一些问题(它获得了“旧”新行)。 Here we "cheat" a little and we use scanf to get a string that can have up to one character. 在这里,我们“作弊”了一点,我们使用scanf来获得一个字符串,该字符串最多可以包含一个字符。 A string of 1 character clearly needs a second character for the terminator, so an array of 2 characters. 一个1字符的字符串显然需要第二个字符作为终止符,因此需要2个字符的数组。

Be aware that using a scanf for the character to search, you won't be able to insert the space character. 请注意,使用scanf搜索字符时,将无法插入空格字符。

Note that gets is an "evil" function. 请注意, gets是一个“邪恶”函数。 You can easily do buffer overruns using it (it doesn't check that the buffer is big enough). 您可以使用它轻松地进行缓冲区溢出(它不会检查缓冲区是否足够大)。 The "right" way to do it is normally: fgets(mystr, N, stdin); 通常,“正确”的方法是: fgets(mystr, N, stdin); (the "file" variant of gets has a maximum number of characters that can be read and will append a \\0 at the end). gets的“文件”变体具有最多可读取的字符数,并将在末尾附加\\0 )。 Note that if you insert 150 characters in a fgets , 99 will go to your string (because you gave 100 of max size), 1x \\0 will be appended and the other characters will remain in the buffer "ready" for the next scanf / gets / fgets ... (to test it, reduce the buffer to a smaller value, like 5 characters, and do some tests) 请注意,如果您在fgets插入150个字符,则字符串中将包含99个字符(因为您提供了100个最大大小),将附加1x \\0 ,其他字符将保留在缓冲区“ ready”中,以便进行下一个scanf / gets / fgets ...(进行测试,将缓冲区减小到一个较小的值,例如5个字符,并进行一些测试)

You can use fgets() as suggested by xanatos with a small hack, so you can reliably handle return characters. 您可以按照xanatos的建议使用fgets()并进行一些改动,以便可靠地处理返回字符。 Just change the '\\n' to '\\0' in the string obtained using fgets. 只需将使用fgets获得的字符串中的'\\ n'更改为'\\ 0'。

And in your program, you forgot to terminate the new string with a '\\0'. 并且在程序中,您忘记了以'\\ 0'终止新字符串。 So here's the code you're looking for. 所以这是您要查找的代码。

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

#define N 100

int main(int argc,char **argv){

char string[N],str1[N];
char ch;
int i,k = 0;

fgets(string,N,stdin);
string[strlen(string)-1] = '\0';
scanf("%c",&ch);

printf("\n%s , %c",string,ch);
for (i=0;i<=strlen(string);i++)
    if(string[i] != ch)
        str1[k++] = string[i];  

str1[k] = '\0'; 
printf("\n%s , %s\n",string,str1);
return 0;
}

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

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