Accenture Coding Problem #1 | Frequency Count

Accenture logo

Today we are going to discuss some important Accenture Coding Questions and answers that has been asked in previous years for engineering Fresher hiring.

So, If you are also appearing in Accenture, then there are fair chances that you can get same difficulty level of questions in your actual exam.

Accenture logo
Accenture Coding Problem 1

Given a string, find the frequencies of each of the characters in it.
The input string contains only lowercase letters. The output string should contain a letter followed by its frequency, in the alphabetical order (from a to z).

Input specification:
Input 1: The input string.

Output Specification:
Return a string representing the frequency counts of characters in the input string.

Example 1:
Input 1: babdc

Output: a1b2c1d1

Explanation:
In the input string, ‘a’ appears once, ‘b’ appears twice, ‘c’ and ‘d’ appear once.

Solution: In C

#include <stdio.h>
#include <string.h>

void printFrequency(int freq[])
{
    for (int i = 0; i < 26; i++) {
        if (freq[i] != 0) {
            printf("%c%d",i+'a', freq[i]);
        }
    }
}

void findFrequncy(char S[])
{
    int i = 0;
    int freq[26] = { 0 };
    while (S[i] != '\0') {
        freq[S[i] - 'a']++;
        i++;
    }
    printFrequency(freq);
}

int main()
{
    char S[100];
    scanf("%s",S);
    findFrequncy(S);
}

Output:

babdc
a1b2c1d1

Leave a Reply

Your email address will not be published. Required fields are marked *