c++ program for : | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

c++ program for :

accept a string and print the count of each characters in a string which consists of numbers,alphabets,special characters

21st Nov 2018, 3:49 PM
jahnavi mantripragada
jahnavi mantripragada - avatar
6 Answers
+ 3
I understand you. I can give you some hints to warm up your mind. Ask yourself these questions: 1. How many printable ASCII character do we have ¹ and what is the numeric range of them ²? printable ASCII chars = { ' ', '!', '"', '#', '
#x27;, ..., '{', '|', '}', '~' } 2. How should a string break down to its building blocks? Considering that the string is a sequence of characters, then the individual chars can be accessed by the bracket operator? string s = "Hello!"; cout << "First character of s is: " << s[0]; 3. What about the last char? Is there a method to retrieve the length of a string? int length_of_s = s.length(); cout << "Last char of s is: " << s[length_of_s - 1]; 4. Can I use a loop for traversing the whole string and extracting each char? char current_char; for (unsigned i = 0; i < size_of_s; ++i) { current_char = s[i]; } 5. How to "store" the number of each char in the whole string, now? What's the easiest way to do so? Can an array help me? How should I construct an array to hold printable chars? how many cells do I require to set aside for that? Are 128 cells suffice ¹? int table[128] = {0}; // char type is a subset of int type. There's a good reason for choosing the table type int instead of char. all cells set to zero. 6. How to establish a link between all these items to make it work? What construct do I have to add to the above for loop's body or change? for (unsigned i = 0; i < size_of_s; ++i) { int ascii_value_of_char = s[i]; // H -> 72 // e -> 101 // l -> 108 // o -> 111 ++table[ascii_value_of_char]; // increase the content of the cell indicated by the current ascii value } [To be continued...] _____ ¹ http://www.maestramarta.it/coding-codice-binario/ascii-0-127/ ² http://facweb.cs.depaul.edu/sjost/it212/documents/ascii-pr.htm
21st Nov 2018, 8:28 PM
Babak
Babak - avatar
+ 3
Have you tried to come up with a solution? If not please take your time and do that, otherwise, ours won't help you to understand the problem and as a matter of fact, you won't appreciate the achievement of a decent solution.
21st Nov 2018, 3:54 PM
Babak
Babak - avatar
+ 3
[...Continue] 7. How should I print out the desired result? (I'm running out of juice! 8D) for (int i = 32; i < 128; ++i) { if (table[i] == 0) continue; else std::cout << "number of '" << (char)i << "' character is: " << table[i] << std::endl; } 8. How does it look all together?! (Bonus) 8D #include <iostream> #include <string> int main() { const int TableLength = 128; const int PrintableStart = 32; int table[TableLength] = {0}; std::string s = "Hello!"; for (unsigned i = 0; i < s.length(); ++i) { int ascii_value_of_char = s[i]; ++table[ascii_value_of_char]; } for (int i = PrintableStart; i < TableLength; ++i) { if (table[i] == 0) continue; else std::cout << "number of '" << (char)i << "' character is: " << table[i] << std::endl; } } Output: number of '!' character is: 1 number of 'H' character is: 1 number of 'e' character is: 1 number of 'l' character is: 2 number of 'o' character is: 1
21st Nov 2018, 8:30 PM
Babak
Babak - avatar
+ 2
Thank you so much I will check it out 😃
22nd Nov 2018, 1:14 AM
jahnavi mantripragada
jahnavi mantripragada - avatar
+ 2
The explanation is very clear😊
22nd Nov 2018, 1:21 AM
jahnavi mantripragada
jahnavi mantripragada - avatar
+ 1
Thank you I tried it I am not getting an idea how to print the characters without repeating them
21st Nov 2018, 4:33 PM
jahnavi mantripragada
jahnavi mantripragada - avatar