-2

こんにちは、コマンドライン引数として「./prog7x hello there」が入力された場合、次のように表示するプログラムを作成しています。

argument 0 is "./prog7x", has length 8, contains 5 alphabetic characters 
argument 1 is "hello", has length 5, contains 5 alphabetic characters 
argument 2 is "there", has length 5, contains 5 alphabetic characters 
Total length 18: ./prog7xhellothere 

アルファベットの数え方に困っています。長さを取得する関数がありますが、長さを計算した後にカウントされた文字を表示する方法がわかりません。これまでのプログラムは次のとおりです...コーディングを始めてまだ数か月しか経っていないので、アドバイスをいただければ幸いです。 !

#include <cctype> //isalpha
#include <cstdio> 
#include <cstring> //strlen
#include <cstdlib>

//Function to display what argument we're on
void displayArgument(char* arr1[],int num);

//Funtcion to get the length of a command line argument then,
//display number of alphabetical characters it contains
void displayLength(char* arr[],int length);

//Function to count the total length, concatenate together,
//and display results
//void displayTotalCat(char* arr2[],int total);

int main(int argc, char* argv[])
{      
    displayArgument(argv,argc);
    displayLength(argv,argc);


    return 0;
}

//Function to display what argument we're on
void displayArgument(char* arr1[],int num)
{
  for(int i=0; i<num; i++) {
       printf("Argument %d is ",i); //what argument we're on
       printf("'%s'\n",arr1[i]);
      } 
}

//Funtcion to get the length of a command line argument then,
//display number of alphabetical characters it contains
void displayLength(char* arr[],int length)
{
  for (int l=0; l<length; l++) {        //what length that position is
       int len=strlen(arr[l]); //what is the length of position l
       printf("Length is %d,\n",len);   //print length
    for(int j=0; j< len ;j++) {
      int atoi(strlen(arr[l][j]));
      printf("Contains %d alphabetical characters",arr[l][j]);
     }
    }

}

//Function to count the total length, concatenate together,
//and display results
//void displayTotalCat(char* arr2[],int total)
4

1 に答える 1

0

結果だけを知りたい場合は最後までスキップしてください。でも、一緒に見ていきましょう。コードの問題のある部分は次のとおりです。

for(int j=0; j< len ;j++) {
    int atoi(strlen(arr[l][j]));
    printf("Contains %d alphabetical characters",arr[l][j]);
}

現在、ループ内で印刷しています。それでは、その部分を引き出してみましょう。

for(int j=0; j< len ;j++) {
  int atoi(strlen(arr[l][j]));
 }
 printf("Contains %d alphabetical characters",arr[l][j]);

偉大な。arr[l][j]また、ループの外 ( jスコープ外) で出力することはできなくなったため、事前に何らかの変数を宣言する必要があります。これは、文字が英数字であると判断したときにこの変数に追加する必要があるため、カウントするのにも役立ちます。

int alphas = 0;
for(int j = 0; j < len; j++) {
    if(????){
        alphas = alphas + 1;
    }
}
printf("Contains %d alphabetical characters", alphas);

コードも少しフォーマットしたことに注意してください。一般に、プログラマーは、スペース、インデント、名前付けなどに関する規則に従って、他の人がコードを読みやすくします。では、文字が英数字かどうかをどのように判断するのでしょうか? 一連の if ステートメント (たとえば、if(arr[l][j] == '1')など) を使用できますが、それはあまりスマートではありません。あなたが調べたのは正しかったですisalpha!まず、これをファイルの先頭に追加します。

#include <ctype.h>

isalpha次に、次のように関数を呼び出すことができるはずです。

int alphas = 0;
for(int j = 0; j < len; j++) {
    if(isalpha(arr[l][j])){
        alphas = alphas + 1;
    }
}
printf("Contains %d alphabetical characters", alphas);
于 2016-11-17T01:34:50.047 に答える