i need to Design and write a program to count the number of occurrences of each of the characters ‘A’ to ‘Z’, ‘a’ to ‘z’ and ‘0’ to ‘9 entered through stdin.
i was thinking of defining a struc for char lower char upper and int digit
but then how do i count this
am really confused any help would be great

I would do it by using an array of 255 ints, each element in the array represents one of the characters in standard ascii character set. So, if you enter 'A', then all you have to do is increment array['A']++. When done, all the non-zero elements are what you want. Now, you know that many of the 255 elements will be unused, so you can limit the array to only 126 or fewer. If the array has 126 elements (values above 126 can not be entered on the standard English keyboard).

Member Avatar for iamthwee

Clue, don't think about this as being either ints or chars.

They are all really chars.

They are all really chars.

There really is no such thing as a char in C and C++ languages, the data type char is just a one-byte integer with a range of values from 0 to 255 (unsigned) or -127 to 127 (signed). The compiler treats 'A' as an integer, not as a character.

I would use 3 "bin" variables. If the value of a char(int) > 46 and < 58 it would be a digit and I would increment a digitbin variable. Same test for upper and lower case letters.
Wonder what would be faster, this strategy or AD's?

Recalling the above good insights of @Ancient Dragon ...

I would do it by using an array of 255 ints, each element in the array represents one of the characters in standard ascii character set. So, if you enter 'A', then all you have to do is increment array['A']++. When done, all the non-zero elements are what you want. Now, you know that many of the 255 elements will be unused, so you can limit the array to only 126 or fewer. If the array has 126 elements (values above 126 can not be entered on the standard English keyboard).

Just in case you do not 'see' what is 'implicitly needed' in that approach ...

// MUST firstly initial all elements to 0

int ary[128] = { 0 }; // initial all to zero ...

// then traverse all char's 'c' ...
// and update ...
if( (c >= 'A' && c << 'Z') || (c >= 'a' && c << 'z')
|| (c >= '0' && c << '9') )
++ ary[c];

Actually I didn't have in mind summing them all up into one of three bins (as ddanbe called them). If that is the case then his approach would be more efficient.

It depends how you interpret the OPs original question.
I'd interpret it in the same way as ddanbe. Rather than wanting to count each occurrence of each individual character in those ranges, the OP wants to count the following:
- The number of numeric digit characters
- The number of lowercase alphabetic characters
- The number of uppercase alphabetic characters

So as far as I see it, the OP needs to define three counter variables and then inside the loop that processes the characters he needs to use the following functions:
isdigit - to determine if it is 0-9, if so - increment the appropriate counter
islower - to determine if it is lowercase, if so - increment the appropriate counter
isupper - to determine if it is uppercase, if so - increment the appropriate counter
The functions named above are all found in ctype.h.

I really don't think anything more complicated is called for!
However, if my interpretation of this problem is incorrect, then AD's advice is spot on!

Thank you for your help
I am a novice at c and it's really cleared things up. The approach I have to take is the method which AD has stated.
By counting each occurrence of a lower case letter e.g 'a' and separately counting each occurrence of capital 'A' .
Also counting how many times number 1 appears
So if this string was entered via stdin
" Abbbb cccc 1111 2"
The output should be
A= 1
b=4
c= 4
1=4
2=1

So ...

if you want the count of the numbers at the end ...

(as per your example output)...

after you have tallied up all elements in your array ...

you could use 3 loops for the printout:

int i;

for( i = 'A'; i <= 'Z'; ++ i )
{
     if( ary[i] ) /* if NOT zero ... */
         printf( "%c = %d\n", i, ary[i] );
}
for( i = 'a'; i <= 'z'; ++ i )
{
     if( ary[i] ) /* if NOT zero ... */
         printf( "%c = %d\n", i, ary[i] );
}
for( i = '0'; i <= '9'; ++ i )
{
     if( ary[i] ) /* if NOT zero ... */
         printf( "%c = %d\n", i, ary[i] );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.