Newer
Older
exercism-cpp / acronym / acronym.cpp
@Wook Song Wook Song on 30 Oct 2022 580 bytes Easy: Add a solution for "Acronym"
#include "acronym.h"

#include <string>

namespace acronym {
std::string acronym(std::string phrase) {
  std::string acronym = "";
  std::string word = "";

  for (auto iter = phrase.begin(); iter != phrase.end(); ++iter) {
    if (word.length() > 0 &&
        ((iter + 1 == phrase.end()) || *iter == ' ' || *iter == '-')) {
      char letter = word[0];

      if (letter >= 'a' && letter <= 'z') {
        letter = 'A' + letter - 'a';
      }
      acronym += letter;
      word = "";
      continue;
    }

    word += (*iter);
  }

  return acronym;
}

}  // namespace acronym