Categories
Programming

C++ String Manipulation

Unlike Java, the C++ string class has no toLowerCase member function. Write a regular function toLowerCase. You may use any loop you like. You may also use the character functions in the <cctype> header.

#include <string>
#include <cctype>
using namespace std;

string toLowerCase(const string& s)
{
   string result;
   for(int i = 0; i < s.size(); ++i)
   {
      result += tolower(s.at(i));
   }
   return result;
}

Unlike Java, the C++ string class does not have the member functions startsWith or endsWith. Write your own function:

    endsWith(const string& a, const string& b)

which returns true if a ends with b. You do not need a loop for this problem, but you may use one if you like.

#include <string>
using namespace std;

   bool endsWith(const string& a, const string& b)
   {
      bool result = false;
      auto alen = a.size();
      auto blen = b.size();
      int pos{0};
      if(a == b)
      {
         result = true;
         return result;
      }
      else if(alen > blen)
      {
         pos += (alen - blen);
         string temp = a.substr(pos);
         if(temp == b)
         {
            result = true;
         }
         else
         {
            result = false;
         }
         return result;
      }
      else
      {
         result = false;
         return result;
      }
   }

It’s raining frogs and fleas. Write a function that tells you if the number of frogs in a sentence is the same as the number of fleas.

#include <string>
#include <cctype>
using namespace std;

bool fleasNFrogs(const string& s)
{
   bool result{false};
   int frogCount{0}, fleaCount{0};
   for(int i = 0; i < s.size(); ++i)
   {
      if(i + 3 < s.size())
      {
         if(s.at(i) == 'f')
         {
            if(s.at(i + 1) == 'r' && s.at(i + 2) == 'o' && s.at(i + 3) == 'g')
            {
               frogCount += 1;
            }
         }
      }
   }
   
   for(int i = 0; i < s.size(); ++i)
   {
      if(i + 3 < s.size())
      {
         if(s.at(i) == 'f')
         {
            if(s.at(i + 1) == 'l' && s.at(i + 2) == 'e' && s.at(i + 3) == 'a')
            {
               fleaCount += 1;
            }
         }
      }
   }
   
   if(frogCount == fleaCount)
   {
      result = true;
   }
   return result;
}

Given a string, return a new string where there are i copies of each individual character, where i is the loop index. (That is there will be 0 copies of the first character, 1 of the second, and so on.)

#include <string>
using namespace std;

string expando(const string& s)
{
   string result;
   auto len = s.size();
   auto j{0};
   for(auto i = 0; i < len; ++i)
   {
      j = 0;
      while(j < i)
      {
         result += s.at(i);
         j++;
      }
   }
   return result;
}

Return the number of times that the string "code" appears anywhere in the given string, except, we’ll accept any letter for the 'd', so "cope" and "cooe" both count.

#include <string>
using namespace std;

int countCode(const string& s)
{
   int count{0};
   for(int i = 0; i < s.size(); ++i)
   {
      if(i + 3 < s.size())
      {
         if(s.at(i) == 'c' && s.at(i+1) == 'o' &&  s.at(i+3) == 'e' )
         {
            ++count; 
         }
      }
   }
return count;
}

Write a function named addCommas that accepts a string representing a number and returns a new string with a comma at every third position, starting from the right. For example:

addCommas("12345678") -> "12,345,678"

strings.cpp

#include <string>
using namespace std;

string addCommas(const string& s)
{
   string result = s;
   auto length = s.size();
   if(length < 4)
   {
      result = s;
   }
   else
   {
      int inserted = 0;
      int i = length % 3;
      if(i == 0)
      {
        i = 3;
      }
      for(; i < length + inserted; i = i + 4)
      {
        result.insert(i, ",");
        inserted++;
      }
   }
   return result;
}

strings.h

#include <string>
#ifndef STRINGS_H
#define STRINGS_H
std::string addCommas(const std::string& s);
#endif

client.cpp

#include <iostream>
using namespace std;

#include "strings.h"

int main()
{
    cout << "What number to process: ";
    string n;   // note string input
    cin >> n;
    
    cout << addCommas(n) << endl;
}