How do I increment letters in c++?
Solution 1
This snippet should get you started. letter
is a char
and not an array of char
s 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!
Solution 2
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.
Solution 3
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
Solution 4
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 ;)
Solution 5
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
Marobri
Updated on July 09, 2022Comments
-
Marobri almost 2 years
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;
-
Linus Kleen over 12 yearsThis just outputs
a
and afterwards incrementsletter
. -
Lightness Races in Orbit over 12 yearsWhy would you cast before incrementing?
-
Roee Gavirel over 12 yearsYeah, fix it. (-1 it's kinda harsh, isn't it?)
-
Linus Kleen over 12 yearsI'd like to remove my -1 but it's locked now unless the answer is edited :-(
-
Sammy S. over 12 yearsThere is no such type in C or C++ ... You're thinking of Java or C# right now.
-
deW1 over 9 yearsCaesars Chiffre is +3 not +13 btw.
-
saagarjha over 6 yearsThis relies on implementation defined behavior, since C and C++ don't guarantee that the alphabetical characters are contiguous.
-
johnsyweb over 6 yearsSurely ASCII and Unicode standards have this covered?
-
saagarjha over 6 yearsYes, it does, but C is designed to portable–this means it runs on computers that may not be fortunate enough to use ASCII or Unicode.
-
Peter over 6 years@Johnsyweb - Saagar Jha is correct. There are real-world standardised character sets, and C++ implementations that work with them. With such implementations, your solution will not work. For people who are willing to assume ASCII or Unicode your solution is fine, but such code will be a bug waiting to be exposed at run time (since it would not be diagnosed by a compiler) when ported to another system.