簡體   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