
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Frequency of Each Digit by Concatenating ASCII Values of Given String
In this problem, we need to count digits frequencies after merging the ASCII values of all characters.
The approach to solving the problem is to create a string containing each character's ASCII values and count the frequency of the digits in the string.
Problem statement - We have a string alpha containing different characters, and the length of the string is N. We need to calculate each digit's frequency after concatenating the ASCII values of the given string's characters.
Sample examples
Input
alpha = "tutorialspoint"
Output
4 25 1 0 1 3 3 2 1 1
Explanation - After concatenating the ASCII values of each character, we get the '11611711611111410597108115112111105110116' string.
We have counted the frequency of the digits in the ASCII string.
The frequency of 0 is 4, 1 is 25, 3 is 1, 4 is 0, and so on.
Input
alpha = "a";
Output
0 0 0 0 0 0 0 1 0 1
Explanation - The resultant ASCII string is 97. So, we printed the frequency of the digits accordingly.
Input
alpha = "123";
Output
1 1 0 0 1 2 0 0 0 1
Explanation - The ASCII string is 495051, and we printed the frequency of digits according to that.
Approach 1
In this approach, we will first create an ASCII string by concatenating each character's ASCII value. After that, we will use the array to count the frequency of each digit in the given string.
Algorithm
Step 1 - Define the asc_str string variable and initialize it with the empty string.
Step 2 - Start traversing the given string.
Step 3 - Convert character to ASCII value using the type casting, and convert integer ASCII value to the string. After that, append the ASCII string value to the asc_str string.
Step 4 - Now, we need to count the frequency of the digits in the asc_str string. So, define the 'dgFreq' array of size 10 and initialize it with zeros.
Step 5 - Start traversing the asc_str string, and update the array element according to the digit.
Step 6 - Print the frequency of each digit one by one.
Example
#include <bits/stdc++.h> using namespace std; void findDigitFreq(string alpha) { // To store the ASCII value of the given string string asc_str = ""; // Traverse the string for (int p = 0; p < alpha.size(); p++) { // Get ASCII value of current character and concatenate with the string asc_str += to_string((int)alpha[p]); } // Array to store dgFreq of digits int dgFreq[10] = {0}; // Traverse asc_str for (int p = 0; p < asc_str.size(); p++) { // Increase digit frequency by 1 dgFreq[asc_str[p] - '0']++; } // Show frequency of all digits for (int p = 0; p < 10; p++) { cout << dgFreq[p] << " "; } } int main() { string alpha = "tutorialspoint"; findDigitFreq(alpha); return 0; }
Output
4 25 1 0 1 3 3 2 1 1
Time complexity - O(N) to get the ASCII value of each character.
Space complexity - O(N) to create the asc_str string.
We learned to get the ASCII value of a character and count the frequency of each digit in the string using the given problem. Programmers may count the total number of odd and even digits in the resultant string for more practice.