Q:

Finding repetition of string pattern in a given string

Could someone answer the below C++ interview question:
Given string:     "song singing in hindi"
find repeated strings like as below :
single characters repetition:

Count of "s" = 2   Count of "o" = 0     Count of "n" = 5    Count of "g" = 3
  .....  etc
two character repetition 

Count of "so" = 0    Count of "on" = 0     Count of "ng" = 3     Count of "in" = 4.....  etc
Three character repetition 

Count of "son" = 0 ....Count of "ing" = 1....  etc
Four character repetition 

Count of "song" = 0   .....etc
Rs

A:

Or even a recursive function would do. To remove the 'patterns' with a white space you can follow Vlad's approach with the string::find function.
#include <iostream>
#include <string>
#include <map>

class FRQ {
    void Func(std::map<std::string, size_t> &frequencyTable, const std::string &m_str, const int stepping = 1) {
        if (stepping == m_str.size()) {
            frequencyTable[m_str]++;
            return;
        }

        for (std::string::size_type i = 0, iMAX = m_str.size(); i < iMAX; ++i) {
            frequencyTable[m_str.substr(i, stepping)]++;
        }

        Func(frequencyTable, m_str, stepping + 1);
    }
public:
    std::map<std::string, size_t> *operator()(const std::string &str) {
        std::map<std::string, size_t> *fTable = new std::map<std::string, size_t>();

        Func(*fTable, str);

        return fTable;
    }
};

int main(void) {
    using namespace std;

    string s = "HiYo HiYo";

    FRQ frq;
    map<string, size_t> *frequenceTable = frq(s);

    cout << "Patterns: " << frequenceTable->size() << endl;
    for (const auto& ptr : *frequenceTable)
        cout << "[ '" << ptr.first << "'::" << ptr.second << " ]" << endl;

    delete frequenceTable;

    return 0;
}

