2012-10-26 37 views
5

質問はhereと尋ねられました。違いは、スペースを削除し、結果の文字列/ char配列を返す関数に引数を渡す必要があることです。スペースを削除するコードがありますが、何らかの理由で元の配列から残っている文字が残っています。私もstrncpyを試しましたが、私は多くのエラーを抱えていました。ここでCの文字列/文字配列から空白を削除する関数

は、私がこれまで持っているものです。

#include <stdio.h> 
#include <string.h> 
#define STRINGMAX 1000              /*Maximium input size is 1000 characters*/ 

char* deblank(char* input)             /* deblank accepts a char[] argument and returns a char[] */ 
{ 
    char *output=input; 
    for (int i = 0, j = 0; i<strlen(input); i++,j++)      /* Evaluate each character in the input */ 
    { 
     if (input[i]!=' ')             /* If the character is not a space */ 
      output[j]=input[i];            /* Copy that character to the output char[] */ 
     else 
      j--;               /* If it is a space then do not increment the output index (j), the next non-space will be entered at the current index */ 
    } 
    return output;               /* Return output char[]. Should have no spaces*/ 
} 
int main(void) { 
    char input[STRINGMAX]; 
    char terminate[] = "END\n";            /* Sentinal value to exit program */ 

    printf("STRING DE-BLANKER\n"); 
    printf("Please enter a string up to 1000 characters.\n> "); 
    fgets(input, STRINGMAX, stdin);           /* Read up to 1000 characters from stdin */ 

    while (strcmp(input, terminate) != 0)         /* Check for que to exit! */ 
    { 
     input[strlen(input) - 1] = '\0'; 
     printf("You typed: \"%s\"\n",input);        /* Prints the original input */ 
     printf("Your new string is: %s\n", deblank(input));     /* Prints the output from deblank(input) should have no spaces... DE-BLANKED!!! */ 

     printf("Please enter a string up to 1000 characters.\n> "); 
     fgets(input, STRINGMAX, stdin);          /* Read up to another 1000 characters from stdin... will continue until 'END' is entered*/ 
    } 
} 
+0

の可能性のある重複した[C言語で与えられた文字列からすべてのスペースとタブを削除する方法?](http://stackoverflow.com/questions/1514660/how-to-remove-all-spaces-and -ab-from-a-given-string-in-c-language) –

答えて

11

あなたはNULターミネーター(\0)新しい長さが元の文字列以下であるためにそれを終了していないinputから空白を除去した後。他の人が述べたように、同じ文字列を送信元と送信先、および文字列の最後の両方に使用されて

char* deblank(char* input)           
{ 
    int i,j; 
    char *output=input; 
    for (i = 0, j = 0; i<strlen(input); i++,j++)   
    { 
     if (input[i]!=' ')       
      output[j]=input[i];      
     else 
      j--;          
    } 
    output[j]=0; 
    return output; 
} 
+0

私のためにうまくいった!ありがとう。関数のスコープの可視性を与えるために、j宣言をforループから移動しなければならなかった。 –

10

あなたがそこに古い尾を残している、あなたは出力を終了していない、それは縮小している可能性があるため。

また、ループ内で常にインクリメントされ、現在の文字がコピーされていない場合は手動でデクリメントする必要があるjの処理を推奨します。それはあまり明確ではないし、無意味な仕事(インクリメントj)をしていて、それが望ましくないときに元に戻す必要があっても。かなり紛らわしい。

それが書かれた簡単だとして:

char * deblank(char *str) 
{ 
    char *out = str, *put = str; 

    for(; *str != '\0'; ++str) 
    { 
    if(*str != ' ') 
     *put++ = *str; 
    } 
    *put = '\0'; 

    return out; 
} 
+0

私はあなたの答えを好みましたが、私は幼児のポインタの理解しか持っていません。私の授業で読んでいる本は、自分のような絶対初心者のために物事をよく説明していないし、正直私のインストラクターに起こっていることを説明することができませんでした。私はあなたのコードを十分に見ていれば、あなたがしたことを一緒にまとめることができますが、私は本当にあなたのコードの仕組みの基本的な概念を持っていません。たとえば、 "++"インクリメントはchar配列でどのように機能しますか?私はそれが数値データ型のためだけに使えると思った。とにかくあなたの入力をありがとう! –

0

が維持されていません。

はちょうどあなたのforループ終わりの時に、それをNULで終了します。

次のようにすることもできます。

char* deblank(char* input)             /* deblank accepts a char[] argument and returns a char[] */ 
{ 
    char *output; 
    output = malloc(strlen(input)+1); 

    int i=0, j=0; 
    for (i = 0, j = 0; i<strlen(input); i++,j++)      /* Evaluate each character in the input */ 
    { 
     if (input[i]!=' ')             /* If the character is not a space */ 
      output[j]=input[i];            /* Copy that character to the output char[] */ 
     else 
      j--;               /* If it is a space then do not increment the output index (j), the next non-space will be entered at the current index */ 
    } 

    output[j] ='\0'; 
    return output;               /* Return output char[]. Should have no spaces*/ 
} 
0

あなたが一度に複数の文字をフィルタリングする必要がある場合はループブロックのための

char* deblank(char* input)             
{ 
char *output=input; 
for (int i = 0, j = 0; i<strlen(input); i++,j++)       
{ 
    if (input[i]!=' ')             
     output[j]=input[i];            
    else`enter code here` 
     j--;                
} 
output[j]='\0'; 
return output;               
} 
0

、あなたが見つけるかもしれない後に(\ 0)ターミネータはnullを追加した後の文字列を返す必要が次のようなものがあります。

char *FilterChars(char *String,char *Filter){ 
    int a=0,i=0; 
    char *Filtered=(char *)malloc(strlen(String)*sizeof(char)); 
    for(a=0;String[a];a++) 
    if(!strchr(Filter,String[a])) 
     Filtered[i++]=String[a]; 
    Filtered[i]=0; 
    return Filtered; 
} 

便利です。あなたが取り除く*フィルターの文字のリストを提供するだけです。たとえば、タブ、改行、およびスペースの場合は "\ t \ n"とします。

0

このコードは、O(n)の時間複雑さで機能します。

char str[]={"my name is Om"}; 
int c=0,j=0; 
while(str[c]!='\0'){ 
    if(str[c]!=' '){ 
     str[j++]=str[c]; 
    } 
    c++; 
} 
str[j]='\0'; 
printf("%s",str); 
関連する問題