Функция возвращает значение

Разберём, что означают фразы «функция возвращает значение» , «тип возвращаемого значения» , «void функция» и подобные

#include <string> #include <iostream> std::string reverse_words(std::string str) { std::string rev_str = str; int str_len = str.length(); int k = 0; int j = 0; for (int i = 0; i < str_len; ++i) { if ((str[i] == 0x20) and (str[i - 1] != 0x20)) { for (j = 0; j < (i - k); j++) { rev_str[j + k] = str[i - j - 1]; } k = i + 1; } else if ((str[i] == 0x20) and (str[i - 1] == 0x20)) { ++k; } // проверяем последний элемент строки if ((i == str_len - 1) and (str[i] != 0x20)) { // если это не пробел, нужен дополнительный reverse for (j = 0; j <= (i - k); j++) { rev_str[j + k] = str[i - j]; } } } for (int i = 0; i < str_len; ++i) { std::cout << rev_str[i]; } return rev_str; } int main() { std::string testString1 = "Visit website www.TopBicycle.ru"; std::string testString2 = "Visit website www.HeiHei.ru "; reverse_words(testString1); reverse_words(testString2); return 0; }

Результат

tisiV etisbew ur.elcyciBpoT.www tisiV etisbew ur.ieHieH.www C:\Users\ao\source\repos\exercise.exe (process 20648) exited with code 0. Press any key to close this window . . .

Решение более грамотного программиста

std::string reverse_words(std::string str) { std::string out; std::string cword; for (char c : str) { if (c == ' ') { out += cword; out += c; cword = ""; continue; } cword = c + cword; } out += cword; return out; }

Автор статьи: Андрей Олегович