C 删除字符串中的字符

C 删除字符串中的字符,c,string,C,String,我正在尝试编写一个代码,要求用户输入一个字符串,并获取除字母以外的所有字符 现在我自己做了,它似乎不能正常工作。我对弦很陌生,所以我试着去理解和掌握弦。我尝试在mac上使用gdb,但我没有所有的功能来理解这一点。 你能帮忙吗 代码必须执行的操作:用户输入(例如):h**el(l)o&^w 输出为hello. 这是我的密码: #include <stdio.h> #include <string.h> int main() { char string[100];

我正在尝试编写一个代码,要求用户输入一个字符串,并获取除字母以外的所有字符

现在我自己做了,它似乎不能正常工作。我对弦很陌生,所以我试着去理解和掌握弦。我尝试在mac上使用gdb,但我没有所有的功能来理解这一点。 你能帮忙吗

代码必须执行的操作:用户输入(例如):
h**el(l)o&^w

输出为
hello.

这是我的密码:

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

int main()
{
   char string[100];
   int i;
   int seen = 0;

   printf("Enter String: ");
   scanf("%s", string);

   for (i=0; string[i]!='\0'; i++)
   {
       if (((string[i]<='a' || string[i]>'z')&&(string[i]<='A' || string[i]>'Z')) ||string[i]!='\0')
       {
           seen = 1;
       }
       else
           seen = 0;
   }
   if (seen==0)
   {
       printf("%s", string);
   }
}
#包括
#包括
int main()
{
字符串[100];
int i;
int=0;
printf(“输入字符串:”);
scanf(“%s”,字符串);
对于(i=0;字符串[i]!='\0';i++)
{
if(((字符串[i]'z')&&(字符串[i]'z'))| |字符串[i]!='\0')
{
seen=1;
}
其他的
seen=0;
}
如果(见==0)
{
printf(“%s”,字符串);
}
}

嗯,您的代码有两个重要问题:

  • 迭代时没有检查边界…如果我键入101个字符的字符串呢?还有4242个字符的字符串
  • 下一个问题是
    scanf(“%s”,…)
因此,基本上,您希望使用
fgets()
而不是
scanf()

但是为什么不一个字符一个字符地获取输入,并构建一个只包含所需字符的字符串呢?它更简单、更灵活

基本上:

#include <ctype.h>

int main() {
    char* string[100];
    int i=0;

    printf("Enter your string: ");

    do {
        // getting a character
        char c = getchar();
        // if the character is alpha
        if (isalpha(c) != 0)
            // we place the character to the current position and then increment the index
            string[i++] = c;
        // otherwise if c is a carriage return
        else if (c == '\r') {
            c = getchar(); // get rid of \n
            // we end the string
            string[i] = '\0'
        }else if (c == '\n')
            // we end the string
            string[i] = '\0';
    // while c is not a carriage return or i is not out of boundaries
    } while (c != '\n' || i < 100);
    // if we've got to the boundary, replace last character with end of string
    if (i == 100)
        string[i] = '\0';
    // print out!
    printf("Here's your stripped string: %s\n", string);

    return 0;
}
或者简单地使用
ctypes
isalpha()
函数,就像我们的两个例子一样

没有部分(删除额外字符)可以更改代码中的字符串

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

char *filter(char *string, int (*test)(int)) {
    char *from, *to;
    for(to = from = string;*from;++from){
        if(test(*from))
            *to++ = *from;
    }
    *to = '\0';
    return string;
}

int main(){
    char string[100];
    printf("Enter String: ");
    scanf("%99s", string);
    printf("%s\n", filter(string, isalpha));

    return 0;
}
#包括
#包括
#包括
字符*过滤器(字符*字符串,int(*测试)(int)){
字符*从,*到;
for(to=from=string;*from;++from){
如果(测试(*自))
*to++=*from;
}
*to='\0';
返回字符串;
}
int main(){
字符串[100];
printf(“输入字符串:”);
scanf(“%99s”,字符串);
printf(“%s\n”,过滤器(字符串,isalpha));
返回0;
}

你能告诉我你得到了什么样的输出,而不是你所期望的吗。。i、 e.有什么问题?wee程序跳过我的语句并关闭。
fgets
如果您想读取多个值,可能是更好的选择。对于单个字符串,我不认为它比
scanf(“%99s”,string)
@zmo更能防止缓冲区溢出。非常感谢您的努力,从我看来,这也是一个好主意,但我主要是试图理解和尝试做复杂的事情。如果它不起作用,最好能理解出什么问题。但请始终记住,您编写的代码行越少,越不复杂,出现的bug就越少……而且以后重新阅读代码就越容易!“你在迭代时没有检查边界”-他在迭代时通过寻找空终止符来检查边界,这很好。但是,他应该使用
fgets
而不是
scanf
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char *filter(char *string, int (*test)(int)) {
    char *from, *to;
    for(to = from = string;*from;++from){
        if(test(*from))
            *to++ = *from;
    }
    *to = '\0';
    return string;
}

int main(){
    char string[100];
    printf("Enter String: ");
    scanf("%99s", string);
    printf("%s\n", filter(string, isalpha));

    return 0;
}