Если я введу » ABCD », я хочу, чтобы вывод был » ZABC ».
Если я ввожу » hello world », вывод должен быть » gdkkn vnqkc ».
Я сделал это:
cout << "Enter text" << endl;
cin >> input;`
result_string = ConvertString(input);
cout << endl << "The result is " << endl << result_string << endl;
Но я не знаю, как определить ConvertString
,
Спасибо вам за помощь. 🙂
Вы должны были бы получить доступ к каждому отдельному символу в строке. Строка позволяет получить доступ к памяти в произвольном порядке, так что вы можете сделать это с помощью цикла for.
string input;
cout << "Enter text" << endl;
cin >> input;
//traverse the string
for (int i = 0;i < input.size(); i++)
{
//check for A or a and move it to Z or z respectively
if(input[i] == 65 || input[i] == 97)
input[i] += 26;
//change the value to minus one
input[i]--;
}
cout << endl << "The result is " << endl << input << endl;
return 0;
когда-нибудь слышали о шифре Цезарь?
#include<iostream>
#include<string>
using std::endl;
using std::cout;
using std::cin;int main(){
std::string input;
int count=0, length, n;
cout<<"enter your message to encrypt:\n";
getline(cin,input);
cout<<"enter number of shifts in the alphabet:\n";
cin>>n;
length=input.length();//check for phrase's length
for(count=0;count<length;count++){
if(std::isalpha(input[count])){
//lower case
input[count]=tolower(input[count]);
for(int i=0;i<n;i++){
//if alphbet reaches the end, it starts from the beginning again
if(input[count]=='z')
input[count]='a';
else
input[count]++;
}
}
}
cout<<"your encrypted message: \n"<<input<<endl;
return 0;
}