Answer the question
In order to leave comments, you need to log in
What needs to be corrected in this code so that blood does not come out of the eyes?
I decided to write a program that works according to the following algorithm:
Some algorithm from one string of characters gets a new string as follows. First, the length of the original character string is calculated; if it is even, then the symbol A is added to the middle of the chain of characters, and if it is odd, then the symbol B is added to the beginning of the chain. In the resulting chain of characters, each letter is replaced by the letter following it in the Russian alphabet (A - to B, B - to C etc., and I - on A). The resulting chain is the result of the algorithm.
For example, if the initial chain was the BPM, then the result of the algorithm will be the chain of VGSN, and if the chain was the original PD, then the result of the algorithm will be the chain of RBU.
Given a string of characters TOR. What string of characters will be obtained if the described algorithm is applied to this string twice (i.e., apply the algorithm to this string, and then apply the algorithm again to the result)? Russian alphabet: ABVGDEYOZHZIYKLMNOPRSTUFKHTSCHSHCHYYYYYUYA.
#include<iostream>
using namespace std;
int main(){
//исходная строка и новая
string str = "TOP", newStr = "";
if(str.length()%2) //если нечётная, то в начало добавляем В
newStr = "B"+str;
else{ //иначе в середину А
for(int i = 0; i < str.length()/2; i++)
newStr+=str[i];
newStr+="A";
for(int i = str.length()/2; i < str.length(); i++)
newStr+=str[i];
}
//заменяем буквы
for(int i = 0; i < newStr.length(); i++){
newStr[i] = newStr[i]+1;
}
//output: CUPQ
cout << newStr << endl;
return 0;
}
Answer the question
In order to leave comments, you need to log in
Let's think together:
- Is it reasonable to add one character to a string from a performance point of view?
- Can we solve the problem in one pass through the final array, and not in two in the worst case, as you have now?
- Do we know the length of the final line in advance?
- Can we create a string of a predetermined length in C++?
- Are there ways in modern C++ to traverse a string and transform it without explicitly using additional entities like indexes?
- Can we make the program work for dynamically given input data, and not for compiled ones?
Besides - you have a serious mistake in this line:
newStr[i] = newStr[i]+1;
read the condition.
What can be improved here?
std::string::insert
instead of spelling. newStr[i]+1
does not work for 'I'->'A' and generally depends heavily on the encoding.
#include <iostream>
#include <string>
#include <algorithm>
#include <windows.h>
void prepare(std::string& value, char a, char b)
{
size_t sz = value.size();
if(sz % 2)
value.insert(0, 1, b);
else
value.insert(sz / 2, 1, a);
}
auto shift = [](const char c, const char a)
{
return (c != 'Ё')
? (c != 'Е')
? a + ((c - a + 1) % 32)
: 'Ё'
: 'Ж';
};
std::string& process(std::string& s, const char a, const char b, int repeat)
{
if(!repeat) return s;
prepare(s, a, b);
std::transform(s.begin(), s.end(), s.begin(),
[&](char c){return shift(c, a);
});
return process(s, a, b, repeat - 1);
};
int main()
{
SetConsoleOutputCP(1251);
std::string one("ВРМ");
std::string two("ТОР");
std::cout << process(one, 'А', 'Б', 1) << "\n" // ВГСН
<< process(two, 'А', 'Б', 2) << std::endl; // ГФБРТ
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question