I need to assign the user-entered string from the alphabet I created
using namespace std;
int main() {
setlocale(LC_ALL, "Turkish");
string str1;
string alphabet[29] = {"a", "b", "c", "ç", "d", "e", "f", "g", "ğ", "h",
"ı", "i", "j", "k", "l", "m", "n", "o", "ö", "p",
"r", "s", "ş", "t", "u", "ü", "v", "y", "z"};
cout << "Enter sentence";
getline(cin, str1);
for (int i = 0; i < str1.length(); i++) {
for (int j = 0; j < 29; j++) {
if (alphabet[j].compare(string(1, str1.at(i))))
{
j = (j + 3) % 29;
str1.assign(alphabet);
break;
} // if
} // for(j)
} // for(i)
cout << "Encrypted message is:-->" << str1 << endl;
return 0;
}
each letter in the entered string must be 3 steps. but this code does not work.
Assign is not the right function.
You need another variable, we can say std::string str2
;
Instead of str1.assign(alphabet)
try use str2.append(alphabet[j])
.
At the end of the cycle you will have in the str2 your string encrypted.
Try this code:
int main() {
setlocale(LC_ALL, "Turkish");
string str1;
string str2;
string alphabet[29] = {"a", "b", "c", "ç", "d", "e", "f", "g", "ğ", "h",
"ı", "i", "j", "k", "l", "m", "n", "o", "ö", "p",
"r", "s", "ş", "t", "u", "ü", "v", "y", "z"};
cout << "Enter sentence";
getline(cin, str1);
for (int i = 0; i < str1.length(); i++) {
for (int j = 0; j < 29; j++) {
if (alphabet[j] == str1.substr(i,1))
{
j = (j + 3) % 29;
str2.append(alphabet[j]);
break;
} // if
} // for(j)
} // for(i)
cout << "Encrypted message is:-->" << str2 << endl;
return 0;
}