C++ Cümledeki Kelimeleri Tersine Sıralama

4 June 2021
string reverse_words(string const &s, char const div) {
  string temp = s;
  string ret;
  string found;
  size_t pos = 0;

  do {
    // Finding first occurrence
    pos = temp.rfind(div);
    if (pos > temp.length()) break;

    // If pos found
    ret += temp.substr(pos, temp.length()-1);
    temp = temp.substr(0, pos);
  } while (pos != string::npos);

  // Adding the last part which has no div
  ret += ' ' + temp;
  // Deleting first space
  ret = ret.substr(1, ret.length()-1);

  return ret;
}

Bu kod, verdiğiniz stringin içindeki tüm kelimeleri, vereceğiniz div ayracına göre ayırır ve sondan başa olmak üzere size geri döner.


Published in cpp