2014-02-24 55 views
0

我正在嘗試下面的代碼,但得到一個錯誤的輸出。例如,我輸入「a b c」,我希望結果是「abc」,但結果是中文字符。如何在C中刪除字符串中的空格?

#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 
} 
+4

b [i] =「\ 0」 - 將其更改爲b [i] ='\ 0'; (使用單引號) –

+1

'*句子!= NULL',這乍一看似乎不正確。 'NULL'通常用於指定內存地址。你應該使用''\ 0'' –

+1

我在那裏看到一個'gets'!請**從不**使用'gets'。它被棄用和不安全。 – ajay

回答

1

b在功能範圍內。將結果複製回原始指針。

喜歡的東西:

strcpy(setence, b);

2

你是返回一個本地數組變量(b),當它在main()訪問真實其中調用未定義的行爲。

不要這樣做。

複製新的字符串返回函數結束之前:

strcpy(setence, b); 

一些更多的注意事項:

  1. 它的拼寫sentence
  2. 檢查'\0'而不是NULL
  3. 使用isspace()時轉換爲unsigned int
  4. 終止符是'\0'而不是"\0"
+2

你的意思是'「\ 0」'而不是'「}」' –

+0

@MadHatter是的,當然是一個錯字。謝謝。 – unwind

0

通過此代碼,您正試圖返回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 
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 
} 
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 */ 

這應該工作。