TonyTroev

Strings

Nov 13th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. // Problem 1
  2. int getCountOfWordsInSentence(const string &sentence) {
  3.     int count = 1;
  4.  
  5.     for (const char &symbol : sentence) {
  6.         if (std::isspace(symbol))
  7.             count++;
  8.     }
  9.  
  10.     return count;
  11. }
  12.  
  13. // Problem 2
  14. int getCountOfSentences(const string &text) {
  15.     int count = 0;
  16.  
  17.     for (const char &symbol : text) {
  18.         if (symbol == '.')
  19.             count++;
  20.     }
  21.  
  22.     return count;
  23. }
  24.  
  25. // Problem 3
  26. void reformatText(string &text) {
  27.     unsigned long long index = 0;
  28.  
  29.     for (string::iterator i = text.begin(); i < text.end(); ++i, ++index) {
  30.         if (*i == ',')
  31.             text.insert(index + 1, " ");
  32.     }
  33. }
  34.  
  35. // Problem 4
  36. void deleteMiddleName(string &text) {
  37.     unsigned long long int indexOfFirstPlace = text.find(' ');
  38.     unsigned long long int indexOfLastPlace = text.find(' ', indexOfFirstPlace + 1);
  39.  
  40.     text = text.erase(indexOfFirstPlace, indexOfLastPlace - indexOfFirstPlace);
  41. }
  42.  
  43. // Problem 5
  44. bool bothAreSpaces(const char &l, const char &r) {
  45.     return l == ' ' && r == ' ';
  46. }
  47.  
  48. void removeMultipleSpaces(string &text) {
  49.     string::iterator uniqueIterator = std::unique(text.begin(), text.end(), bothAreSpaces);
  50.     text.erase(uniqueIterator, text.end());
  51. }
  52.  
  53. // Problem 6
  54. void removeSecondSentence(string &text) {
  55.     unsigned long long indexOfFirstDot = text.find('.');
  56.     unsigned long long indexOfSecondDot = text.find('.', indexOfFirstDot + 1);
  57.  
  58.     text = text.erase(indexOfFirstDot, indexOfSecondDot - indexOfFirstDot);
  59. }
  60.  
  61. // Problem 7
  62. char getUpperLetter(const char &letter){
  63.     return static_cast<char>(std::toupper(letter));
  64. }
  65.  
  66. // Problem 8
  67. void changeLetterCasing(string &text){
  68.     for (char &letter : text){
  69.         if (std::isupper(letter))
  70.             letter = static_cast<char>(tolower(letter));
  71.         else if (std::islower(letter))
  72.             letter = static_cast<char>(toupper(letter));
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment