pastebin

Paste Search Dynamic
Recent pastes
int To String Conversion
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. string intToStringConversion(int number)
  7. {
  8.     ostringstream ss;
  9.     ss << number;
  10.     string str = ss.str();
  11.     return str;
  12. }
  13.  
  14. int countApperancesOfChosenSign (string word, int position)
  15. {
  16.     int occurencesNumber = 1;
  17.     if (word.length() > position)
  18.     {
  19.         while (word[position] == word[position + 1])
  20.         {
  21.             occurencesNumber++;
  22.             position++;
  23.         }
  24.     }
  25.     else
  26.         occurencesNumber = 0;
  27.     return occurencesNumber;
  28. }
  29.  
  30. string wordShortening (string shortenWord)
  31. {
  32.     int wordLength = shortenWord.length();
  33.     int occurencesNumber;
  34.     char repeatedSign;
  35.  
  36.     for ( int i = 0; i < wordLength - 2; i++ )
  37.     {
  38.         if ((shortenWord[i] == shortenWord[i + 1]) && (shortenWord[i + 1] == shortenWord[i + 2]))
  39.         {
  40.             occurencesNumber = countApperancesOfChosenSign (shortenWord, i);
  41.             repeatedSign = shortenWord[i];
  42.             shortenWord.replace(i, occurencesNumber, repeatedSign + intToStringConversion(occurencesNumber));
  43.             wordLength = shortenWord.length();
  44.         }
  45.     }
  46.     return shortenWord;
  47. }
  48.  
  49. int main()
  50. {
  51.     int howManyTests;
  52.     string word;
  53.     cin >> howManyTests;
  54.  
  55.     for (int i =0; i < howManyTests; i++)
  56.     {
  57.         cin >> word;
  58.         cout << wordShortening (word) << endl;
  59.     }
  60.     return 0;
  61. }
Parsed in 0.024 seconds