2012-04-02 5 views
2

私はポインタとstrcatをCから使用しようとしています。これは私の学習プロセスの一部です。ポインタ付きstrcat

アイデアは、ユーザーが数字を含む文字列を入力し、出力が数字のみを返すという考えです。 したがって、ユーザが te12abcを入力すると、出力は12になります。

これが私の最初の試みである:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 

#define SIZE 10 

int main() 
{ 
    char palavra[SIZE]; 
    char palavra2[SIZE]; 
    char *pont = palavra; 
    char *pont2 = palavra2; 

    printf("Insert the string\n"); 
    scanf("%s", palavra); 

    do{ 
     if (isdigit(*pont)){ 
      strcat(palavra2, *pont); 
     } 
     *pont++; 
    }while (*pont != '\0'); 

    printf("\nThe number is:\n%s\n", palavra2); 
    return 0; 
} 

私は期待通りにポインタが動作していることを信じているが、strcatのが動作していない理由を理解することはできません。

プログラムが数値を見つけて、その変数を1つの変数に格納し、その変数でstrcatを使用しようとすると、もう一度試みます。コードは次のとおりです。

int main() 
{ 
    char palavra[SIZE]; 
    char palavra2[SIZE]; 
    char temp; 
    char *pont = palavra; 
    char * pont2 = &temp; 

    printf("Insert the string\n"); 
    scanf("%s", palavra); 

    do{ 
     if (isdigit(*pont)){ 
      temp = *pont; 
      strcat(palavra2, pont2); 
     } 
     *pont++; 
    }while (*pont != '\0'); 

    printf("\nThe number is:\n%s\n", palavra2); 
    return 0; 
} 

もう一度、strcatで問題が発生します。

最後に1つ試みたが、ポインタがなくてもstrcatが機能しない。コードは次のとおりです。

int main() 
{ 
    int i = 0; 
    char palavra[SIZE]; 
    char palavra2[SIZE]; 
    char temp; 

    printf("Insert the string\n"); 
    scanf("%s", palavra); 

    do{ 
     if (isdigit(palavra[i])){ 
      temp = palavra[i]; 
      strcat(palavra2, palavra[i]); 
     } 
     i++; 
    }while (palavra[i] != '\0'); 

    printf("\nThe number is:\n%s\n", palavra2); 
    return 0; 
} 

正しい方向を教えてください。今私はより多くの何を行うことができます。..

よろしく、

favolas

答えて

1

削除*(間接参照)、

 strcat(palavra2, pont); 

strcatのはchar*ないchar が、このバージョンを期待しないでください残り全体を追加します。 ヌル終了文字列を作成する必要があります。

と*無用です

*pont++; 

これは、ジョブ

pont++; 

は今、すべてを一度に

int main() 
{ 
    char palavra[SIZE]; 
    char palavra2[SIZE]; 
    char c2[2] = "a"; 
    char *pont = palavra; 
    char *pont2 = palavra2; 

    printf("Insert the string\n"); 
    scanf("%s", palavra); 

    do{ 
    if (isdigit(*pont)){ 
     c2[0] = *pont; 
     strcat(palavra2, c2); 
    } 
    pont++; 
}while (*pont != '\0'); 

printf("\nThe number is:\n%s\n", palavra2); 
return 0; 

しかし、これは

int main() 
{ 
    char palavra[SIZE]; 
    char palavra2[SIZE]; 


    printf("Insert the string\n"); 
    scanf("%s", palavra); 

    char *pont = palavra; 
    char *pont2 = palavra2; 

    while (true) { 
    char c = *pont ++; 
    if (c == 0) break; 
    if (isdigit(c)){ 
     *pont2++ = c; 
    } 
    }; 
    printf("\nThe number is:\n%s\n", palavra2); 
    return 0; 
複雑すぎるん
3

あなたはそれをもっと難しくしています。あなたはstrcatは必要ありません。

/* Untested. */ 
char *pont = palavra; 
char *pont2 = palavra2; 

while (*pont) { 
    if (isdigit(*pont)) 
     *pont2++ = *pont; 

    pont++; 
} 
*pont2 = 0; 
+0

小さな問題は、私が理解し、OPはので、例えば、文字列内のすべての数字を追加したいと思います"a1b2c3d"は3つの追加を行います。 'while(* pont){if(isdigit(* pont)){* pont2 ++ = * pont; } ++ pont; } * pont2 = 0; '、と思います。 –

+0

@ダニエルフィッシャーああ、私は参照してください。公正で十分です、ありがとう:-) – cnicutar

+0

@cnicutarありがとうこれは動作しますが、 'te1te2te3te4te5'を挿入するとプログラムがクラッシュします。文字列のサイズが10未満であると定義されていると思われます。 – Favolas

3

あなた3回目はほとんど正しいです。

私はかつてのcosをpalavra+iなくpalavra[i]を渡しています

strncat(palavra2, palavra+i,1); 

strcat(palavra2, palavra[i]); 

を交換進んポインタであり、後者は文字とstrncatニーズポインタ

でこれがあります説明する良い例文字列と文字を連結する方法

また、あなたのコードで

char palavra[SIZE]=""; 
char palavra2[SIZE]=""; 
+0

'strcat(palavra2、palavra + i、1)'エラー:関数に引数が多すぎます 'strcat'' – Favolas

+0

'strncat'とnot strcat' :) –

+0

私の悪い:)それは動作しますが、問題は他のすべてと同じです。 'te1te2te3te4te5'を挿入すると、プログラムがクラッシュする – Favolas

3

Porblemsあなたは常にあなたの変数を初期化します(第1版)

1)あなたはstrcatを行うが、*pontがある、単一文字を指し、ヌルで終了する文字列ではありません。

2)実行する*pont++; * pontは値でありポインタではありません。

第1版に変更してください。これは問題ありません。

do{ 
     if (isdigit(*pont)){ 
      *pont2=*pont; 
      pont2++; 
     } 
     pont++; 
    }while (*pont != '\0'); 

*pont2='\0'; 
+0

pont2は、charがコピーされた場合にのみインクリメントされます(つまり、isdigit(* pont)) – eyalm

+0

ありがとうございます。今修正する。 –

+0

もちろん、終了は* pont2 = 0にする必要があります。 – eyalm