Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem 1
- int getCountOfWordsInSentence(const string &sentence) {
- int count = 1;
- for (const char &symbol : sentence) {
- if (std::isspace(symbol))
- count++;
- }
- return count;
- }
- // Problem 2
- int getCountOfSentences(const string &text) {
- int count = 0;
- for (const char &symbol : text) {
- if (symbol == '.')
- count++;
- }
- return count;
- }
- // Problem 3
- void reformatText(string &text) {
- unsigned long long index = 0;
- for (string::iterator i = text.begin(); i < text.end(); ++i, ++index) {
- if (*i == ',')
- text.insert(index + 1, " ");
- }
- }
- // Problem 4
- void deleteMiddleName(string &text) {
- unsigned long long int indexOfFirstPlace = text.find(' ');
- unsigned long long int indexOfLastPlace = text.find(' ', indexOfFirstPlace + 1);
- text = text.erase(indexOfFirstPlace, indexOfLastPlace - indexOfFirstPlace);
- }
- // Problem 5
- bool bothAreSpaces(const char &l, const char &r) {
- return l == ' ' && r == ' ';
- }
- void removeMultipleSpaces(string &text) {
- string::iterator uniqueIterator = std::unique(text.begin(), text.end(), bothAreSpaces);
- text.erase(uniqueIterator, text.end());
- }
- // Problem 6
- void removeSecondSentence(string &text) {
- unsigned long long indexOfFirstDot = text.find('.');
- unsigned long long indexOfSecondDot = text.find('.', indexOfFirstDot + 1);
- text = text.erase(indexOfFirstDot, indexOfSecondDot - indexOfFirstDot);
- }
- // Problem 7
- char getUpperLetter(const char &letter){
- return static_cast<char>(std::toupper(letter));
- }
- // Problem 8
- void changeLetterCasing(string &text){
- for (char &letter : text){
- if (std::isupper(letter))
- letter = static_cast<char>(tolower(letter));
- else if (std::islower(letter))
- letter = static_cast<char>(toupper(letter));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment