testsAndMisc/CPP/miscelanious/howOftenDoesCharOccur.cpp
Krzysztof kuhy Rudnicki f6b6995b0e Add tests and fix pre-commit issues across all projects
- C/lichess_random_engine, vocabulary_curve, misc/split,
  1dvelocitysimulator, opening_learner: test suites added
- CPP/miscelanious: tests added
- TS/battery-status, champions_leauge_scores, two-inputs: tests added
- python_pkg/fm24_searcher, wake_alarm: new packages added
- Fix ruff/cppcheck/eslint/clang-format failures
- Update .gitignore for C/C++ build artifacts
2026-04-12 20:45:24 +02:00

45 lines
1.2 KiB
C++

#include "howOftenDoesCharOccur.h"
#include <iostream>
#include <vector>
void printCharOccurenceVector(const std::vector<charOccurence> v) {
std::cout << "[";
for (unsigned int i = 0; i < v.size(); i++) {
std::cout << "(\"" << v.at(i).c << "\", " << v.at(i).occurrence << ")"
<< (i + 1 == v.size() ? "" : ", ");
}
std::cout << "]" << std::endl;
}
std::vector<charOccurence> computeCharOccurences(const std::string &userInput) {
std::vector<charOccurence> list;
if (!userInput.empty()) {
charOccurence newCharOccurence;
newCharOccurence.c = userInput.at(0);
newCharOccurence.occurrence = 1;
for (unsigned int i = 1, j = 1; i < userInput.length(); i++) {
char newCharacter = userInput.at(i);
if (newCharacter != newCharOccurence.c) {
list.push_back(newCharOccurence);
j = 1;
newCharOccurence.c = newCharacter;
newCharOccurence.occurrence = j;
} else {
newCharOccurence.occurrence++;
}
}
list.push_back(newCharOccurence);
}
auto result = list;
return result;
}
#ifndef TESTING
int main() {
std::string userInput = "aaaabbbcca";
std::vector<charOccurence> list = computeCharOccurences(userInput);
printCharOccurenceVector(list);
return 0;
}
#endif