当前位置: 动力学知识库 > 问答 > 编程问答 >

function - c++ Working with Command Line Arguments.. isalpha not working and how to concat together

问题描述:

Hello I'm making a program to display the following if say './prog7x hello there ' was entered as command line argument:

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

I'm having trouble with counting the alphabetic characters.

I have a function to get the length, but I don't understand how to display the character's counted after length is done.. here's the program so far...I've only been coding for a couple months so any advice is appreciated!

#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)

网友答案:

Skip to the end if you just want the result, but let's walk through this together. Here is the problematic part of your code:

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

Currently, you are printing inside your loop. So let's pull that part out:

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

Great. Also, we can no longer print arr[l][j] outside of the loop (j is out of scope) so we will need some kind of variable declared beforehand. This also makes sense to help us count, since we will want to add to this variable when we determine a character is alphanumeric:

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

Notice that I also formatted your code a little. In general, programmers follow rules about spaces, indentation, naming etc. to make their code easier for others to read. So, how do we determine if a character is alphanumeric? We could use a series of if statements (e.g. if(arr[l][j] == '1') etc.) but that's not very smart. You were right to look into isalpha! First, add this to the top of your file:

#include <ctype.h>

Then, you should be able to call the isalpha function like this:

int alphas = 0;
for(int j = 0; j < len; j++) {
    if(isalpha(arr[l][j])){
        alphas = alphas + 1;
    }
}
printf("Contains %d alphabetical characters", alphas);
分享给朋友:
您可能感兴趣的文章:
随机阅读: