Are you sure you must use a declared struct?
If you just want to count the number of times a character has appeared, you don't need to store the character; you just need to store the number of times it appeared. So just unsigned lettersCount[26]
, and each index maps to a letter (i.e. index 0
means a
, index 1
means b
). Whenever a letter appears, just increase the count of that index.
You can map a letter to the index by making use of ASCII. Every letter is represented by a decimal number that you can look it up at ASCII table. For example, the letter a
is represented by the decimal value 97, b
is 98 and so on. The number increases successively, which we can make use of. So if you want to map a letter to an index, all you need to do is just value - 97
or value - 'a'
. For example, if you read in the letter a
, take away 97 from that and you'll get 0
, which is what you want. After getting the index, it's just a simple ++
to increment the count of that letter.
Regarding the treatment of uppercase and lowercase (i.e. treat them the same or differently), it'll be up to you to figure it out how to do it (which should be fairly simple if you can understand what I've explained).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…