3

如何从字符串中删除空格特殊字符?

谷歌搜索时我找不到一个答案。有很多与其他语言相关,但与 C 无关。其中大多数都提到了正则表达式的使用,这不是 C 标准(?)。

删除一个简单的空间很容易:

 char str[50] = "Remove The Spaces!!";

然后是一个带有 if 语句的简单循环:

if (str[i] != ' ');

输出将是:

RemoveTheSpaces!!

我要在 if 语句中添加什么,以便它能够识别特殊字符并删除它们?

我对特殊字符的定义:

Characters not included in this list: 
A-Z a-z 0-9
4

7 回答 7

7

这可能不是实现这一目标的最有效方法,但它会相当快地完成工作。

注意:此代码确实要求您包含<string.h><ctype.h>

char str[50] = "Remove The Spaces!!";
char strStripped[50];

int i = 0, c = 0; /*I'm assuming you're not using C99+*/
for(; i < strlen(str); i++)
{
    if (isalnum(str[i]))
    {
        strStripped[c] = str[i];
        c++;
    }
}
strStripped[c] = '\0';
于 2013-03-16T01:39:16.867 回答
1

这只是一个愚蠢的建议。

char ordinary[CHAR_MAX] = {
    ['A']=1,['B']=1,['C']=1,['D']=1,['E']=1,['F']=1,['G']=1,['H']=1,['I']=1,
    ['J']=1,['K']=1,['L']=1,['M']=1,['N']=1,['O']=1,['P']=1,['Q']=1,['R']=1,
    ['S']=1,['T']=1,['U']=1,['V']=1,['W']=1,['X']=1,['Y']=1,['Z']=1,

    ['a']=1,['b']=1,['c']=1,['d']=1,['e']=1,['f']=1,['g']=1,['h']=1,['i']=1,
    ['j']=1,['k']=1,['l']=1,['m']=1,['n']=1,['o']=1,['p']=1,['q']=1,['r']=1,
    ['s']=1,['t']=1,['u']=1,['v']=1,['w']=1,['x']=1,['y']=1,['z']=1,

    ['0']=1,['1']=1,['2']=1,['3']=1,['4']=1,['5']=1,['6']=1,['7']=1,['8']=1,
    ['9']=1,
};

int is_special (int c) {
    if (c < 0) return 1;
    if (c >= CHAR_MAX) return 1;
    return !ordinary[c];
}

void remove_spaces_and_specials_in_place (char *str) {
    if (str) {
        char *p = str;
        for (; *str; ++str) {
            if (!is_special(*str)) *p++ = *str;
        }
        *p = '\0';
    }
}
于 2013-03-16T02:03:23.800 回答
1

有数百万种不同的方法可以做到这一点。这只是一个不使用任何额外存储并“就地”删除不需要的字符的示例:

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

static void my_strip(char *data)
{
    unsigned long i = 0; /* Scanning index */
    unsigned long x = 0; /* Write back index */
    char c;

    /*
     * Store every next character in `c` and make sure it is not '\0'
     * because '\0' indicates the end of string, and we don't want
     * to read past the end not to trigger undefined behavior.
     * Then increment "scanning" index so that next time we read the
     * next character.
     */
    while ((c = data[i++]) != '\0') {
        /* Check if character is either alphabetic or numeric. */
        if (isalnum(c)) {
            /*
             * OK, this is what we need. Write it back.
             * Note that `x` will always be either the same as `i`
             * or less. After writing, increment `x` so that next
             * time we do not overwrite the previous result.
             */
            data[x++] = c;
        }
        /* else — this is something we don't need — so we don't increment the
           `x` while `i` is incremented. */
    }
    /* After all is done, ensure we terminate the string with '\0'. */
    data[x] = '\0';
}

int main()
{
    /* This is array we will be operating on. */
    char data[512];

    /* Ask your customer for a string. */
    printf("Please enter a string: ");

    if (fgets(data, sizeof(data), stdin) == NULL) {
        /* Something unexpected happened. */
        return EXIT_FAILURE;
    }

    /* Show the customer what we read (just in case :-)) */
    printf("You have entered: %s", data);

    /*
     * Call the magic function that removes everything and leaves
     * only alphabetic and numberic characters.
     */
    my_strip(data);

    /*
     * Print the end result. Note that newline (\n) is there
     * when we read the string
     */
    printf("Stripped string: %s\n", data);

    /* Our job is done! */
    return EXIT_SUCCESS;
}

我在那里放了很多评论,所以希望代码不需要解释。希望能帮助到你。祝你好运!

于 2013-03-16T02:06:52.813 回答
0

这是ASCII码范围

Char:Dec

0:48, 9:57
A:65, Z:90
a:97, z:122

试试这个:

char str[50] = "Remove The Spaces!!";

int i =0;
for(; i<strlen(str); i++)
{
    if(str[i]>=48 && str[i]<=57 || str[i]>=65 && str[i]<=90 || str[i]>=97 && str[i]<=122)
  //This is equivalent to
  //if(str[i]>='0' && str[i]<='9' || str[i]>='A' && str[i]<='Z' || str[i]>='a' && str[i]<='z')
        printf("alphaNumeric:%c\n", str[i]);
    else
    {
        printf("special:%c\n", str[i]);
        //remove that
    }
}
于 2013-03-16T01:38:37.187 回答
0

使用你的 if 语句:

if (str[i] != ' ');

有一点逻辑(字符必须在 az 或 AZ 或 0-9 范围内:

If ( !('a' <= str[i] && 'z' >= str[i]) &&
     !('A' <= str[i] && 'Z' >= str[i]) &&
     !('0' <= str[i] && '9' >= str[i])) then ignore character.
于 2013-03-16T01:45:00.330 回答
0
#include <stdio.h>
#include <string.h>

main()
{
    int i=0, j=0;
    char c;
    char buff[255] = "Remove The Spaces!!";

    for(; c=buff[i]=buff[j]; j++){
       if(c>='A' && c<='Z' || c>='a' && c<='z' || c>='0' && c<='9'){
           i++;
       }
    }

    printf("char buff[255] = \"%s\"\n", buff);
}
于 2014-05-19T18:29:42.577 回答
0
include < stdio.h >

int main()
{
    char a[100];

    int i;
    printf("Enter the character : ");
    gets(a);
    for (i = 0; a[i] != '\0'; i++) {
        if ((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z') 
             || (a[i] - 48 >= 0 && a[i] - 48 <= 9)) {
            printf("%c", a[i]);
        } else {
            continue;
        }
    }
    return 0;
}
于 2019-01-15T08:23:55.480 回答