Моё решение
#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;
}
Результат
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;
}
Задача - совершить прогулку в 10 минут. Можно сделать
10 коротких переходов по 1 минуте в любом направлении. n - север, s - юг,
w - запад, e - восток
На вход функция должна принять вектор произвольной длины.
Моё решение
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;
}
Решение более грамотного программиста
#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');
}
Задача:
В файле input.txt любая строка либо содержит в каком-то месте ; либо пустая.
Всё, что слева от первого знака ; нужно скопировать в файл
output.txt
Если строка пустая, её нужно записать как пустую строку в новый файл.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib> // для использования функции exit()
int main()
{
std::string line;
std::string new_line;
new_line = "";
char buff[1];
std::ifstream file("C:\Users\andreyolegovichru\input.txt"); // окрываем файл для чтения
// Если мы не можем открыть этот файл для записи данных в него,
if (!file)
{
// то выводим сообщение об ошибке и выполняем функцию exit()
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(); // закрываем файл
std::cout << "End of program" << std::endl;
return 0;
}
Share in social media:
|