繁体   English   中英

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

[英]how to remove space in a string in C?

我正在尝试下面的代码,但输出错误。 例如,我键入“ abc”,我希望结果为“ 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
}

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

不要这样

函数结束之前,将新字符串复制回去:

strcpy(setence, b);

更多注意事项:

  1. 这是拼写的sentence
  2. 检查'\\0' ,而不是NULL
  3. 使用isspace()时强制转换为unsigned int
  4. 终止符是'\\0' ,而不是"\\0"

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

就像是:

strcpy(setence, b);

使用此代码,您将尝试返回b ,该b是在stack上定义的。 一旦超出范围,它将不会反映在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

您不能在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
}
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 */

这应该工作。

暂无
暂无

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

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