-2

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)
alpacaness
  • 21
  • 1

1 Answers1

0

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);
Eric M.
  • 563
  • 3
  • 15
  • Thank you so much for taking the time to write all that out. You made my night! – alpacaness Nov 17 '16 at 01:46
  • Glad it helped! Keep at it, you're on the right path. Sorry about the people downvoting. – Eric M. Nov 17 '16 at 01:49
  • Any chance you want to provide more guidance? I'm trying to get the lay out of the program to match the example that's above. When I run the command for example: ' ./a.out hi ' The output is all on separate lines rather than: Argument 0 is './a.out' length is 7, alpha characters etc... Should I have one function to do all of this? Or should I be returning values back to main to display all the output in main? I'm currently stuck on concatenating and displaying the total length of the input because of the for loops and the variables being local. Thanks again in advance! – alpacaness Nov 17 '16 at 09:37
  • It would be easier to return the values back to main, which handles all printing. Alternatively you could use a global variable to aggregate all the lengths and then print it out after you're done with your loop. Also, it's the "\n" at the end of your `printf` statements which adds the new lines, by the way, so you can just remove that anywhere you don't want a new line after printing. – Eric M. Nov 17 '16 at 15:01