Task: This cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet.
For example:
string = "Abc Def Ghi 999 -*%/&()[]"
shift(number that you entered) = 1(Can be any integer)
Program should print like this: Bcd Efg Hij -*%/&()[]
I did this with a void function but once I tried to do same thing with string function it didn't work. It just process first element of string then returns the value. For this particular situation
my program prints like "Bbc Def Ghi 999 -*%/&()[]"
Any idea to solve this problem?
#include <iostream>
#include <cmath>
using namespace std;
string Cipher(string password, int shift) {
char Uppercase[26] = { 'A','B','C' ,'D' ,'E' ,'F' ,'G' ,'H' ,'I' ,'J' ,'K' ,'L' ,'M' ,'N' ,'O','P' ,'Q' ,'R' ,'S' ,'T' ,'U' ,'V' ,'W' ,'X' ,'Y','Z' };
char Lowercase[26] = { 'a','b','c' ,'d' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o','p' ,'q' ,'r' ,'s' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y','z' };
for (int i = 0; i < password.length(); i++) {
for (int k = 0; k < 26; k++) {
int add = shift + k;
if (password[i] == Uppercase[k]) {
for (int i = 0; add > 25; i++) { // controlling add isn't bigger than 25
add -= 25;
}
password[i] = Uppercase[add]; // converting each letter
}
else if (password[i] == Lowercase[k]) {
for (int i = 0; add > 25; i++) { // controlling add isn't bigger than 25
add -= 25;
}
password[i] = Lowercase[add]; //converting each letter
}
else {
k = 25; // if element of string is different from letters, program goes to next element
}
}
}
return password;
}
int main() {
cout << "Please enter an integer different from 0 and multiples of 25: ";
string password = "Abc def ghi 999 -*%/&()[]";
int shift;
cin >> shift;
cout<< Cipher(password, shift);
system("pause>0");
}
Your encryption problem can be solved with one statement by using modern C++.
But because this is somehow advanced, I will give a detailed explanation.
Let us first think, what to do and then, how to implement.
The what:
Only alpha characters shall be converted
None alpha characters shall be output unencrypted (same as input)
In case of encryption, the case of the original letter shall be preserved for the encrypted letter. Meaning, if at position 5 was an uppcase letter, also in the encrypted string the letter in position 5 shall be uppercase
Letters shall be shifted by a specified amount.
The how:
We will first check, it the original letter is an alpha-letter by using the isalpha-function.
So, for the case that it is an alpha letter, we will check, if the letter is in uppercase or in lowercase. Actually, we check only, if it is a uppercase letter.
Because if it is not, then it must be a lowercase letter (because it was definitely a letter, what we did check before, and, if it is not upper, then it is lower case). For this check, we use the isupper-function.
We will then do the shift action. And convert back to a letter, taken the case into account.
We assume ASCII. If we to convert an ASCII letter/character into a 0 based index, we need to do the following:
If we look in the ASCII table, then we see, that and 'A' is equivalent to 65 and so forth. So, to get the 0-based index, we subtract 65 from the letter. Then we have an index value between 0 and 25.
Then we add the shift value. There could of course be an overflow. But, this can be simply corrected by a modulo 26 division.
So: 26->0, 27->1, 28->2 and so on. Rather simple. But, because we want to have later a letter again, we will add 65 to this result.
For lowercase letters, we will do nearly the same, but use 97 for the offset to letter 'a'.
Then, we can put verything in one expresion by using the ternary or conditional-operator.
std::isalpha(c) ? std::isupper(c) ? (c - 65 + shift) % 26 + 65 : (c - 97 + shift) % 26 + 97 : c
This is a short for
// Check if letter/character is alpha
if (std::isalpha(c)) {
// Now check for upper or lower case
if (std::isupper(c)) {
// The character is uppercase
c = (c - 65 + shift) % 26 + 65;
}
else {
// The character is lower case
c = (c - 97 + shift) % 26 + 97;
}
}
else {
// The character is not alpha
}
Same as the one liner, no difference
So, first, check for alpha. If true, check for uppercase, if true, do the conversion for uppcase letter, else for lower case letters. If it was no alpha letter, then leave it unchanged.
All this we will then embed as a Lambda-expresion in a std::transform-statement. Please see here for a description.
The result will be one statement only, for the whole conversion process:
std::transform(password.begin(), password.end(), password.begin(), [shift](const char c)
{return std::isalpha(c) ? std::isupper(c) ? (c - 65 + shift) % 26 + 65 : (c - 97 + shift) % 26 + 97 : c; });
At the end, we build a small driver program for demo purposes:
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main() {
// Our password
std::string password = "Abc def ghi 999 -*%/&()[]";
// Give instruction to the user
std::cout << "\nPlease enter a postive integer: ";
// Get number of shifts from user and check, if the value could be read
if (int shift{}; std::cin >> shift && shift > 0) {
// Now do the encryption
std::transform(password.begin(), password.end(), password.begin(), [shift](const char c)
{return std::isalpha(c) ? std::isupper(c) ? (c - 65 + shift) % 26 + 65 : (c - 97 + shift) % 26 + 97 : c; });
// Show the result to the user
std::cout << "\n\nEncrypted passphrase: \t" << password << '\n';
}
else std::cerr << "\n\n*** Error: Problem with input!\n\n";
return 0;
}
And, since the one liner is maybe too advanced, let's use the explicit and more verbose code. Just to be complete:
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main() {
// Our password
std::string password = "Abc def ghi 999 -*%/&()[]";
// Give instruction to the user
std::cout << "\nPlease enter a postive integer: ";
// Get number of shifts from user and check, if the value could be read
if (int shift{}; std::cin >> shift && shift > 0) {
// --------------Can be written in one statement -----------------------
for (char& c : password) {
// Check if letter/character is alpha
if (std::isalpha(c)) {
// Now check for upper or lower case
if (std::isupper(c)) {
// The character is uppercase
c = (c - 65 + shift) % 26 + 65;
}
else {
// The character is lower case
c = (c - 97 + shift) % 26 + 97;
}
}
else {
// The character is not alpha
}
// ------------------------------------------------------------------
}
// Show the result to the user
std::cout << "\n\nEncrypted passphrase: \t" << password << '\n';
}
else std::cerr << "\n\n*** Error: Problem with input!\n\n";
return 0;
}
Within your k loop you determine the index of the letter in the alphabet. However, when e.g. i=1 then password[1] represents the letter 'b'. Now, starting the k-loop from k==0 where Uppercase[0] and Lowercase[0] represent 'A' and 'a', respectively, you directly end up in the else condition and your k-loop terminates without doing anything (you set k=25 and increment it). Here is a fixed version (note that I also use the modulo operator % to make sure that 0 < add < 26:
#include <iostream>
#include <cmath>
using namespace std;
string Cipher(string password, int shift) {
char Uppercase[26] = { 'A','B','C' ,'D' ,'E' ,'F' ,'G' ,'H' ,'I' ,'J' ,'K' ,'L' ,'M' ,'N' ,'O','P' ,'Q' ,'R' ,'S' ,'T' ,'U' ,'V' ,'W' ,'X' ,'Y','Z' };
char Lowercase[26] = { 'a','b','c' ,'d' ,'e' ,'f' ,'g' ,'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o','p' ,'q' ,'r' ,'s' ,'t' ,'u' ,'v' ,'w' ,'x' ,'y','z' };
for (int i = 0; i < password.length(); i++) {
for (int k = 0; k < 26; k++) {
int add = (shift + k)%26;
if (password[i] == Uppercase[k]) {
password[i] = Uppercase[add]; // converting each letter
break;
}
else if (password[i] == Lowercase[k]) {
password[i] = Lowercase[add]; //converting each letter
break;
}
}
}
return password;
}
int main() {
cout << "Please enter an integer different from 0 and multiples of 25: ";
string password = "Abc def ghi 999 -*%/&()[]";
int shift;
cin >> shift;
cout<< Cipher(password, shift);
system("pause>0");
}
Related
How can I replace every letter in a string with opposite letter? For example, replace "a" with "z", replace "B" with "Y". How can I make it for every string?
Please consider this solution:
From the ASCII table http://www.asciitable.com/
CHAR DECIMAL
"A" 65
"B" 66
...
"Y" 89
"Z" 90
If we want to swap 'B' (which is the second char past 'A') for 'Y' (which is the second to last char from 'Z'), we might want to" take the distance from 'A' and subtract that from 'Z', as in 'Z' - (x - 'A')
#include <string>
#include <iostream>
char invert_char(char x)
{
if (x >= 'a' && x <= 'z')
return char('a' -x + 'z');
if (x >= 'A' && x <= 'Z')
return char('A' -x + 'Z');
return x;
}
std::string invert_string(std::string str)
{
for (auto& c: str)
c = invert_char(c);
return str;
}
int main()
{
std::string test = "ABCDEF UVWXYZ";
std::cout << test << std::endl;
std::cout << invert_string(test) << std::endl;
return 0;
}
Use a translation/lookup array.
Define an array (vector) of source letters, and a translation string. You can use a pair of maps to to encode and decode, or you could use brute force string search for every character (both ways shown below.
class translate {
std::string original;
std::string coded;
std::map<char,char> encode_map;
std::map<char,char> decode_map;
translation function (brute force),
public:
// you could pass original and coded string pair, or use defaults
translate() {
original="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
coded="ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba";
for( size_t ndx = 0; ndx < order; ++ndx ) {
encode[original.at(ndx)] = coded.at(ndx);
decode[coded.at(ndx)] = original.at(ndx);
}
}
//brute force scans original to find coded position, n*O(m)
std::string
brute(std::string src)
{
std::string dest;
for( auto& ch : src ) {
dest.push_back(coded.at(find(original,ch)));
}
return dest;
}
//use map for encode, decode, n*O(log(m))
std::string
encode(std::string src)
{
std::string dest;
for( auto& ch : src ) {
dest.push_back(encode_map[ch]);
}
return dest;
}
std::string
decode(std::string src)
{
std::string dest;
for( auto& ch : src ) {
dest.push_back(decode_map[ch]);
}
return dest;
}
}
std::transform provides a very convenient way to apply a transformation to every member of a container. With std::string you can easily apply your wanted transformation of grabbing the character from the opposite end of the alphabet by testing if the current character isupper() and if so, grab the character offset from 'Z' the same number of characters as the current character is from 'A'. (same would apply for islower() in case of a lowercase character)
If the character is not an alpha-character, then it remains unchanged. Using std::transform, your function can be reduced to:
void oppositechar (std::string& s)
{
std::transform (s.begin(), s.end(), s.begin(),
[](unsigned char c) {
if (std::isupper(c)) /* if upper */
c = 'Z' - c + 'A'; /* replace w/dist from Z */
else if (std::islower(c)) /* if lower */
c = 'z' - c + 'a'; /* replace w/dist from z */
return c;
});
}
Adding a short program that lets you enter any string as the first argument on the command line (or using "Hello World" if no argument is provided), you could do:
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
void oppositechar (std::string& s)
{
std::transform (s.begin(), s.end(), s.begin(),
[](unsigned char c) {
if (std::isupper(c)) /* if upper */
c = 'Z' - c + 'A'; /* replace w/dist from Z */
else if (std::islower(c)) /* if lower */
c = 'z' - c + 'a'; /* replace w/dist from z */
return c;
});
}
int main (int argc, char **argv) {
std::string s = argc > 1 ? argv[1] : "Hello World";
std::cout << s << '\n';
oppositechar (s); /* transform s with encoding */
std::cout << s << '\n';
}
Example Use/Output
$ ./bin/transformoppositechar
Hello World
Svool Dliow
$ ./bin/transformoppositechar "ABC-XYZ abc-xyz"
ABC-XYZ abc-xyz
ZYX-CBA zyx-cba
Look things over and let me know if I did not understand what you were attempting to do correctly, or if you have further questions.
You can check for each letter in the input string and replace it with the opposite letter manually by writing if statements. But there's a more programmatic way.
In English alphabet there are only 26 letters. To get the inverse string, you will replace any a with z, any b with y. That means you replace 1st letter with 26th letter, 2nd letter with 25th letter. As programmers, we count from 0. So you replace 0th letter with 26th and so on. If the input letter is a, i.e 0th letter, you'll replace it with (26 - 0)th letter. If it is b, i.e 1st letter you'll replace it with (26 - 1)th letter and so on. If it is nth letter, replace it with (26 - n)th letter.
But in ascii table, a is 97th and A is 65th. So we have to substract 97 from ascii value of relevant letter before do the above explained math. Substract 65 if letter is upper case.
#include <iostream>
using namespace std;
string inverse(string data){
string inverse_string = "";
for(char c : data){
int limit = 97;
if (((int)c) < 97) limit = 65; //assumes the letter is uppercase
int chr_code = (int)c - limit;
int inverse_chr_code = 26 - chr_code;
int result = inverse_chr_code + (limit - 1);
inverse_string += (char)result;
}
return inverse_string;
}
int main(){
cout << inverse("apPle");
return 0;
}
Above code gives output "zkKov" i.e the inverse string to "apPle"
Try the following code:
char newChar = 'z' - (oldChar - 'a'); // If oldChar is in between 'a' and 'z'
char newChar = 'Z' - (oldChar - 'A'); // If oldChar is in between 'A' and 'Z'
See this for ASCII table.
You need to handle the situation like oldChar is a valid alphabet ((a <= oldChar and oldChar <= 'z') or (A <= oldChar and oldChar <= 'Z'))
See this code:
#include <iostream>
#include <string>
#include <exception>
char char_invert(char x)
{
if ((x >= 'a') and (x <= 'z'))
return 'z' - (x - 'a');
if ((x >= 'A') and (x <= 'Z'))
return 'Z' - (x - 'A');
throw std::runtime_error("Invalid Character");
}
std::string string_invert(std::string str)
{
std::string result;
for (auto& c: str)
result.push_back(char_invert(c));
return result;
}
int main()
{
std::string example = "aBCDEFUVWXYZ";
std::cout << example << std::endl;
std::cout << string_invert(example) << std::endl;
return 0;
}
try this. explanation of code is in the comments.
//---generic: this supports any string buffer such as const char*.
void invertStrBuf(char* p_szArr, size_t p_size){
for(size_t a = 0; a < p_size; a++){
char item = p_szArr[a];
char sc = item & ~0x20; //force convert to upper case.
if('A' <= sc && sc <= 'Z'){
sc = 'Z' - (sc - 'A'); //invert letter
p_szArr[a] = sc | (item & 0x20); //change back to original case and then save.
}
}
}
//---std::string.
void invertStr(std::string& p_str){
invertStrBuf(&p_str[0], p_str.size());
}
void main(){
//---convert std::string
std::string sVal = "abcZYX123&*(";
invertStr(sVal);
cout << "string: " << sVal << endl;
// //---convert char buffer.
// const char sampVal = "another 123 EXAMPLE +_\[]";
// size_t sizeSampVal = strlen(sampVal);
// char* charBuf = malloc(sizeSampVal+1);
// memmove(charBuf, sampVal, sizeSampVal+1);
// invertStrBuf(&charBuf[0], strlen(charBuf));
// cout << "charBuf[]: " << charBuf << endl;
}
I am writing a C++ console application and I'm turning a into 1, b into 2 and so on. Thing is, it's outputting numbers like 48 and 52 - even though the array I'm basing it off only goes up to 26.
Here's the code:
void calculateOutput() {
while (input[checkedNum] != alphabet[checkedAlpha]) {
checkedAlpha++;
if (checkedAlpha > 27) {
checkedAlpha = 0;
}
}
if (input[checkedNum] == alphabet[checkedAlpha]) {
cout << numbers[checkedAlpha] << "-";
checkedAlpha = 0;
checkedNum++;
calculateOutput();
}
}
Here is my number and alphabet arrays:
char alphabet [27] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','o','p','q','r','s','t','u','v','w','x','y','z',' '};
int numbers [27] = { '1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','0' };
Its int array so it means that it will save ASCII values of characters.
If you would look carefully on ASCII table, you would find out that 48,49,50,... are ascii values of numbers 0,1,2,...
What you have to do is deduct value of first number in table -> '0' (48)
cout << numbers[checkedAlpha] - '0' << "-";
or better, save numbers as numbers not characters
int numbers [27] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18',19,20,21,22,23,24,25,26,0 };
Btw. here is hint which make it easier for you
tolower(inputAlphabet[index]) - 'a' + 1 // For 'a' output is 1 and so on :-)
The algorithm to get the number (i9ndex) of the letters of the alphabet is quite simple. No need for tables, a simple subtraction does the trick.
int getLetterIndex(char c)
{
// returns -1 if c is not a letter
if ('a' <= c && c <= 'z')
return 1 + c - 'a';
if ('A' <= c && c <= 'Z')
return 1 + c - 'A';
return -1;
}
I'm trying to solve problem 8 from project euler but I'm getting way too big numbers as results and I don't know why.
The problem is "Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"
My code :
#include <iostream>
#include <string>
int main()
{
std::string str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
long long a = 1;
long long fin = 0;
for (int c = 0; c < 988; c++)
{
for (int d = 0; d < 13; d++)
{
a = a * str.at(c + d);
}
if (a > fin)
{
fin = a;
std::cout << fin << " at " << c << std::endl;
}
a = 1;
}
system("pause");
}
The output :
7948587103611909356 at 0
8818137127266647872 at 15
8977826317031653376 at 71
9191378290313403392 at 214
9205903071867879424 at 573
Press any key to continue...
The problem is that the characters '0' through '9' are not the same as the integers 0 through 9; rather, '0' has the value 48, '1' has the value 49, and so on. (These are the ASCII values of those characters.)
So to convert from a digit character to the desired number — for example, to extract e.g. 3 from '3' — you need to subtract '0'. In other words, you need to change this:
a = a * str.at(c + d);
to this:
a = a * (str.at(c + d) - '0');
i'm trying to implement Ceaser cipher in c++ language
#include <iostream>
#include <string>
#include <locale>
using namespace std;
int main()
{
string word;
getline(cin,word);
for(int i=0; i<word.length();i++)
{
if(isalnum(word[i]))
{
//shift by 3
word[i]+= 3;
}
}
cout << word ;
return 0;
}
what i want is to limit the output also for only letter and number .
for example if i want to shift z by 3 the output would be 'c' and not '}' as in my code .
Compilers are much better at handling the tedious details than humans, so in this case, I would write the code to show clearly what you intend, and then let the compiler figure out the numbers.
For example, if you want to shift a letter, don't you really just want to add 3 to the index of the letter in the range A to Z, and then mod by 26 -- the number of letters from A to Z? This is really what you want -- rotate around the circle of LETTERS from A to Z, of which there are 26, and not worry about ASCII values.
In that case, you can let the compiler figure it out for you:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int shift = 3;
char* input = "Is it 90 with a ZERO?";
printf("%s\n", input);
int length = strlen(input);
char* output = malloc(length + 1);
output[length] = '\0';
for (int i = 0; i < length; i++)
{
char c = input[i];
if (c >= 'A' && c <= 'Z')
{
c = (((c - 'A') + shift) % 26) + 'A';
}
else if (c >= 'a' && c <= 'z')
{
c = (((c - 'a') + shift) % 26) + 'a';
}
else if (c >= '0' && c <= '9')
{
c = (((c - '0') + shift) % 10) + '0';
}
output[i] = c;
}
printf("%s\n", output);
}
Why would you want to take on that responsibility, if you are not worried about speed or memory footprint?
You have to make sure it does not go out of the valid range for ASCII letters. A way of doing this is to convert the input to lowercase, then make sure that when the shift is added, it does not exceed 122 (z's value in ASCII).
if (word[i] + SHIFT > 'z') {
SHIFT -= 123 - word[i];
word[i] = 'a'; // go back to the beginning
word[i] += SHIFT; // add on the remaining amount
}
This should work.This assumes that there will be only lower case letters.
word[i]+= 3;
//at this point word[i] might have cross the ascii limit for lower case letters
ie may be word[i]>122.Ascii range for lower case letters is 97-122
So we use mod to wrap it around.
But now may be word[i]<97 which is again out of range so we add 97 to it.
word[i]%=123;
if(word[i]<97)word[i]+=97;
Example z
word[i]+=3 makes word[i] as 125 //out of range
word[i]%=123 //word[i]=3
word[i]+=97 //word[i]=99=c
I'm creating a Caesar Cipher in c++ and i can't figure out how to increment a letter.
I need to increment the letter by 1 each time and return the next letter in the alphabet. Something like the following to add 1 to 'a' and return 'b'.
char letter[] = "a";
cout << letter[0] +1;
This snippet should get you started. letter is a char and not an array of chars nor a string.
The static_cast ensures the result of 'a' + 1 is treated as a char.
> cat caesar.cpp
#include <iostream>
int main()
{
char letter = 'a';
std::cout << static_cast<char>(letter + 1) << std::endl;
}
> g++ caesar.cpp -o caesar
> ./caesar
b
Watch out when you get to 'z' (or 'Z'!) and good luck!
It works as-is, but because the addition promotes the expression to int you want to cast it back to char again so that your IOStream renders it as a character rather than a number:
int main() {
char letter[] = "a";
cout << static_cast<char>(letter[0] + 1);
}
Output: b
Also add wrap-around logic (so that when letter[0] is z, you set to a rather than incrementing), and consider case.
You can use 'a'+((letter - 'a'+n)%26);
assuming after 'z' you need 'a' i.e. 'z'+1='a'
#include <iostream>
using namespace std;
int main()
{
char letter='z';
cout<<(char)('a' + ((letter - 'a' + 1) % 26));
return 0;
}
See this https://stackoverflow.com/a/6171969/8511215
Does letter++ work?
All in all char is a numeric type, so it will increment the ascii code.
But I believe it must be defined as char letter not an array. But beware of adding one to 'Z'. You will get '[' =P
#include <iostream>
int main () {
char a = 'a';
a++;
std::cout << a;
}
This seems to work well ;)
char letter = 'a';
cout << ++letter;
waleed#waleed-P17SM-A:~$ nano Good_morning_encryption.cpp
waleed#waleed-P17SM-A:~$ g++ Good_morning_encryption.cpp -o Good_morning_encryption.out
waleed#waleed-P17SM-A:~$ ./Good_morning_encryption.out
Enter your text:waleed
Encrypted text:
jnyrrq
waleed#waleed-P17SM-A:~$ cat Good_morning_encryption.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
//the string that holds the user input
string text;
//x for the first counter than makes it keeps looping until it encrypts the user input
//len holds the value (int) of the length of the user input ( including spaces)
int x, len;
//simple console output
cout << "Enter your text:";
//gets the user input ( including spaces and saves it to the variable text
getline(cin, text);
//give the variable len the value of the user input length
len = (int)text.length();
//counter that makes it keep looping until it "encrypts" all of the user input (that's why it keeps looping while its less than len
for(x = 0; x < len; x++) {
//checks each letts (and spaces) in the user input (x is the number of the offset keep in mind that it starts from 0 and for example text[x] if the user input was waleed would be w since its text[0]
if (isalpha(text[x])) {
//converts each letter to small letter ( even though it can be done another way by making the check like this if (text[x] =='z' || text[x] == 'Z')
text[x] = tolower(text[x]);
//another counter that loops 13 times
for (int counter = 0; counter < 13; counter++) {
//it checks if the letts text[x] is z and if it is it will make it a
if (text[x] == 'z') {
text[x] = 'a';
}
//if its not z it will keeps increamenting (using the loop 13 times)
else {
text[x]++;
}
}
}
}
//prints out the final value of text
cout << "Encrypted text:\n" << text << endl;
//return 0 (because the the main function is an int so it must return an integer value
return 0;
}
Note: this is called caeser cipher encryption it works like this :
ABCDEFGHIJKLMNOPQRSTUVWXYZ
NOPQRSTUVWXYZABCDEFGHIJKLM
so for example my name is waleed
it will be written as : JNYRRQ
so its simply add 13 letters to each letter
i hope that helped you
It works but don't forget that if you increment 'z' you need to get 'a' so maybe you should pass by a check function that output 'a' when you get 'z'.
cast letter[n] to byte* and increase its referenced value by 1.