Problem solving in C++

Rewrite all the words in the line in reverse order without touching the spaces

My solution.

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

Solution by a more experienced 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; }

Check to see if the walk took ten minutes and if the person returned.

The goal is to walk in 10 minutes. It can be done. 10 short transitions of 1 minute in any direction. n - north, s - south, w - west, e - east

The input function must take a vector of arbitrary length.

My decision.

bool isValidWalk(std::vector<char> walk) { int distance = walk.size(); if (distance == 10) { int n{ 0 }; int e{ 0 }; for (int i = 0; i < 10; i++) { if (walk[i] == 'n') { n++; } else if (walk[i] == 's') { n--; } else if (walk[i] == 'w') { e--; } else if (walk[i] == 'e') { e++; } else { continue; } } if ((n == 0) and (e == 0)) { return true; } } else { return false; } return false; }

Solution by a more experienced programmer.

#include<vector> #include <algorithm> bool isValidWalk(std::vector<char> walk) { return walk.size() == 10 and std::count(walk.begin(), walk.end(), 'e') == std::count(walk.begin(), walk.end(), 'w') and std::count(walk.begin(), walk.end(), 'n') == std::count(walk.begin(), walk.end(), 's'); }

Rewriting from file to file

Task:

In the input.txt file, any string either contains a ; somewhere or is empty.

Everything to the left of the first character ; You need to copy it into a file. output.txt

If the line is empty, write it as an empty line in a new file.

#include <iostream> #include <fstream> #include <string> #include <cstdlib> // to use the exit() function int main() { std::string line; std::string new_line; new_line = ""; char buff[1]; std::ifstream file("C:\Users\andreyolegovichru\input.txt"); // cover the file // If we cannot open this file to record data in it, if (!file) { // We send an error message and perform the exit() function. std::cerr << "file could not be opened for writing!" << std::endl; exit(1); } if (file.is_open()) { while (getline(file, line)) { if (!line.length()) { std::cout << "Empty line detected" << std::endl; std::ofstream res_file("C:\Users\wd973579\result.txt", std::ios::app); if (res_file.is_open()) { res_file << "" << std::endl; } res_file.close(); } int line_length = line.length(); for (int i = 0; i < line_length; i++) { char s = line[i]; if (s == ';') { std::cout << new_line << std::endl; std::ofstream res_file("C:\Users\wd973579\result.txt", std::ios::app); if (res_file.is_open()) { res_file << new_line << std::endl; } res_file.close(); new_line = ""; break; } else if (s == '\r') { std::cout << "Empty line detected" << std::endl; } else { std::cout << s << std::endl; } new_line = new_line + s; } } } file.close(); // close the file std::cout << "End of program" << std::endl; return 0; }

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

Article author: Andrei Olegovich