Challenge

+1 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?

Post a Reply

Replies

- page 3
Oldest  Newest  Rating
0 Lazar Bulic · February 16, 2015
Sorry, I forgot about that. 
Just correct the while loop like this: 

while((ch = getchar()) != '\n'){
        if(isalpha((char)ch)){
            if(isupper((char)ch)){
                coun1++;
                sum1 += ch;
            }else{
            coun2++;
            sum2 += ch;
            }
        }else{
            if(isdigit((char)ch)){
                coun3++;
                sum3 += ch;
            }else
                break;
        }
    }


If you can't understand any part of my cod please ask? Also when there are no numbers and caps in the input the printf fill be strage as division by 0 isn't defined but that can be fixed with couple of if statements.
0 J show · February 16, 2015
Thanks a lot!
0 J show · February 16, 2015
alright i have a question now. So get char is getting values and characters. But i think need my while loop to stop whenever i enter a punctuation. Like ! or # $. And scanf wouldnt work for this one? 

C

106,928 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator