如何在C中删除字符串中的空格?

时间:2014-02-24 11:47:32

标签: c string space

我正在尝试下面的代码,但输出错误。例如,我输入“a b c”,我希望结果为“abc”,但结果是中文字符。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
/* function prototype */
char *sweepSpace(char *sentence);
int main()
{
    char str[80];
    printf("Enter a string: ");//enter a string for example "a b c'
    gets(str);
    printf("Result: %s  ", sweepSpace(str));//should print "abc"here 
    return 0;
}
char *sweepSpace(char *setence)
{
    char b[80];     
    int i = 0;
    while (*setence != NULL)
    {
        //if not reach the end
        if (!isspace(*setence))
        {
            //if not a space
            b[i] = *setence;//assign setence to b
            i++;//increment 
        }
        setence++;//pointer increment
    }
    b[i]= "\0";

    return b;//return b to print
}

5 个答案:

答案 0 :(得分:2)

您正在返回一个本地数组变量(b),该变量在main()中访问时会调用未定义的行为。

不要这样做。

在函数结束前复制新字符串:

strcpy(setence, b);

更多说明:

  1. 拼写为sentence
  2. 检查'\0',而不是NULL
  3. 使用unsigned int时投靠isspace()
  4. 终止符为'\0',而不是"\0"

答案 1 :(得分:1)

b的功能范围。将结果复制回原始指针。

类似的东西:

strcpy(setence, b);

答案 2 :(得分:0)

使用此代码,您尝试返回bstack定义main。一旦超出范围,它将不会反映在char b[80]; .. .. return b;//return b to print

new_b

相反,您返回char* new_b = (char*)malloc(strlen(b)+1); strncpy(new_b,b,strlen(b)+1); return new_b;//return b to print

{{1}}

答案 3 :(得分:0)

您无法在C中返回自动数组。当函数sweepSpace返回时,数组b超出范围(它在堆栈上分配),并将内存位置的地址返回到main,该地址不再可用。这将导致未定义的行为,并可能导致段错误。此外,永远不要使用gets。它不检查它写入的缓冲区的边界,如果输入字符串太大,可能会超出缓冲区。请改用fgets。这将再次导致错误。这就是我的建议。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>        // for prototype of isspace
#include <stdlib.h>       // for prototype of malloc

char *sweepSpace(char *sentence);

int main(void)     // parameter list should contain void explicitly
{
    char str[80];  
    printf("Enter a string: "); //enter a string for example "a b c'
    fgets(str, 80, stdin);  // read at most 79 chars. add null byte at the end
    char *new_sentence = sweepSpace(str);
    if(new_sentence) {
        printf("Result: %s  ", new_sentence); //should print "abc" here
        free(new_sentence);
        new_sentence = NULL;
    }
    return 0;
}

char *sweepSpace(char *sentence)
{
    char *b = malloc(1 + strlen(sentence)); // +1 for the null byte
    if(b == NULL) {
        printf("Not enough memory");
        return NULL;
    }     
    int i = 0;
    while(*sentence != '\0')  // compare with null byte, not NULL pointer  
    {
        //if not reach the end
        if (!isspace(*sentence))
        {
            // if not a space

            b[i] = *sentence; //assign sentence to b
            i++;  //increment 
        }
        sentence++; //pointer increment
    }
    b[i]= '\0';   // "\0" is a string literal, not a character.
    return b;  //return b to print
}

答案 4 :(得分:0)

char *a="hello world";
int i;
int w=strlen(a);
for(i=0; i<=w; i++) {
if(*(a+i)==' ') { 
for(j=i;j<=w-1;j++) {
*(a+i)=*(a+1);
} }
}

/* if there is a space, push all next character one byte back */

这应该有效。

相关问题