# Challenge

 J show · February 14, 2015 Write a program that reads alphanumeric characters from the keyboard, and computes the average ascii value of the alpha numeric characters, the average alphabetical character, the average numeric character and the average uppercase character. Outputting each, you program should terminate reading once it read a non-alphanumeric character.How would you guys go about doing this problem?using ctype.h and isalnum?

## Replies

- page 1

 Lazar Bulic · February 17, 2015 Yup that is the solution, gj man J show · February 17, 2015 if((value_character >= 'A' && value_character <= 'Z') || (value_character >= 'a' && value_character <= 'z')) J show · February 17, 2015 ``if((value_character >= 'A' && value_character = 'a' && value_character I think that should fix it?`` J show · February 17, 2015 Oh okay i see! Thanks! Lazar Bulic · February 17, 2015 It isn't that simple as you have few non alpha characters between 'Z' and 'a'. Check J show · February 17, 2015 Oh its a nested loop. So the first one is testing whether it is from big A to little z which will be an alphabetical character and then the second if is nested and is testing whether its an upper case letter i think haha Lazar Bulic · February 17, 2015 just care about your if(value_character >= 'A' && value_character <= 'z');you should have small caps 'a' instead of 'A' J show · February 17, 2015 Alright i got it to work!!Thanks!#include int main(void){ /*Declaring variable types, all are int except the users input*/ char value_character; /*Setting starting values for all variables*/ int digit_loop = 0; int alpha_loop = 0; int upper_loop = 0; int loop = 1; int digit=0; int alpha=0; int upper= 0; /*While loop will continue until the users input is not a letter or number*/ while (loop == 1){ /*Getting a letter or number from the user with the char variable type*/ printf("Enter a alpha numeric character(enter a non aplha numeric character to stop):"); scanf("%c", &value_character); getchar(); /*This if statement will work if the input is a letter*/ if(value_character >= 'A' && value_character <= 'z') { /*increasing the alpha loop by one and storing the alpha value*/ alpha += value_character; alpha_loop++; /*This if statement will work if the input is an upper case letter*/ if(value_character >= 'A' && value_character <= 'Z') { /*increasing the upper loop by one and storing the upper value*/ upper += value_character; upper_loop++; } } /*This if statement will work if the input is a number from one to 9*/ else if (value_character<='9' && value_character >='0') { /*increasing the digit loop by one and storing the digit value*/ digit += value_character; digit_loop++; } else { /*terminates if the input is not a letter or number*/ loop = 0; } }  /*Displaying the averages*/ printf("The average ascii value of the alpha numeric characters is %d.\n", (alpha + digit) / (alpha_loop + digit_loop)); printf("The average numeric character is %c.\n", digit / digit_loop); printf("The average alphabetical character is %c.\n", alpha / alpha_loop); printf("The average uppercase character is %c.\n", upper / upper_loop); return 0;} Lazar Bulic · February 17, 2015 Ok I see you want to work on your code which is totally cool  I will try to help but you have mistakes in it. First of all what is the point of x? I don't get what are u trying with it. I didn't know what was the return value of scanf but I checked it here: http://stackoverflow.com/questions/10469643/value-returned-by-scanf-function-in-c and it turns out you can use it in code but not the way u did. There are much easier ways to achieve it. Also i see that you ditched the isalpha, isdigit and used ascii values instead. (Also loose ctype.h as it isn't needed if this code). This is totally fine. There is an easier way to do it. You don't need to know ascii values of characters or to look it up in the table. For example instead of this``if (value >= 65 && value <= 90)``you can use:``if (value >= 'A' && value <= 'Z')``So as this code has many mistakes I can go tell you them all and try to guild you or I can modify your code (quite a bit) and comment and explain everything for you. Its all about you want J show · February 16, 2015 #include #include int main(void){ int value; int digit_loop = 0; int alpha_loop = 0; int upper_loop = 0; int loop = 1; float digit=0; float alpha=0; float upper= 0; int total; int x; while (loop==1){ printf("Enter a alpha numeric character(enter a non aplha numeric character to stop):"); x = scanf("%d", &value); printf("inside while before if %d\n",x); //printf("%d", x); if (x == 1){ printf("inside if %d\n",x); digit += value; digit_loop++; } else{ printf("inside else %c\n",value); if (value >= 65 && value <= 90) { printf("inside uppercase if %d\n",x); alpha += value; alpha_loop++; upper += value; upper_loop++; } else if(value<=122 && value>=97) { printf("inside lowercase if %d\n",x); alpha += value; alpha_loop++; } else { printf("inside final else %d\n",x); loop = 0; } } }  printf("The average ascii value of the alpha numeric characters is %d.\n", (alpha + digit) / (alpha_loop + digit_loop)); printf("The average numeric character is %c.\n", digit / digit_loop); printf("The average alpha character is %c.\n", alpha / alpha_loop); printf("The average upper case character is %c.\n", upper / upper_loop); return 0;}

## C

126,547 followers