您好,如果输入“./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)