Function returns value

Let us figure out what the phrases «function returns a value», «return value type» and «void function» mean.

#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; } // Check the last element of the string if ((i == str_len - 1) and (str[i] != 0x20)) { // if it is not a space we need to use additional 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; }

The result

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 . . .

The solution of a more competent programmer

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; }

Статьи о C++
Вектор. Часть 0.
Вектор. Часть 1.
Вектор. Часть 2.
Указатели
Классы
SFML
Тетрис на C++ с библиотекой SFML2
SDL
Массив Структур
Как узнать тип переменной C++
Решение задач на C++
Как создать пустую строку в C++
Errors C++

Article author: Andrei Olegovich

Related articles
Development in C++
Function overloading
Splitting code into parts
Vector
Pointers
Classes
SFML
Tetris in C++ with the SFML2 library
SDL
Array of structures
How to find out the type of a variable in C++
Problem solving in C++
How to create an empty line in C++
Errors C++

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: