从字符串中删除空格和特殊字符

时间:2013-03-16 01:28:18

标签: c string

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

谷歌搜索时找不到一个答案。有很多与其他语言相关,但不是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

7 个答案:

答案 0 :(得分: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';

答案 1 :(得分: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';
    }
}

答案 2 :(得分: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;
}

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

答案 3 :(得分:0)

这是Ascii Code Range

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
    }
}

答案 4 :(得分:0)

使用你的if语句:

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

有一点逻辑(字符必须在a-z或A-Z或0-9范围内:

If ( !('a' <= str[i] && 'z' >= str[i]) &&
     !('A' <= str[i] && 'Z' >= str[i]) &&
     !('0' <= str[i] && '9' >= str[i])) then ignore character.

答案 5 :(得分: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);
}

答案 6 :(得分: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;
}