Can u plz amend this code so that it works for shift parameters greater than 6,
ID: 3625657 • Letter: C
Question
Can u plz amend this code so that it works for shift parameters greater than 6,
no complex stuff plz!!(PS shift is a user defined integer, that shifts alphabets forward
for eg. 2 means that A turns to C)
char c;
char encrypt[90]={0}; //don't know how many blanks so must be larger
if(choice==1) //encrypt
{
gets(encrypt);
for(done=0;encrypt[done]!='';done++)
{c=encrypt[done];
if(c!=' ')
{if(c>='a'&&c<='z'||c>='A'&&c<='Z') //only code letters
{c+=shift; //add shift , but if > z must "wrap around"
if(c>'z'||c>'Z'&&c<'a') //ascii A=65, Z=90,a=97,z=122
c-=26;
}
}
cout<}
}
Explanation / Answer
The following code will store the solution back in the encrypt array and output it to the screen. The shift amount is hard-coded but you should be able to take that in from the user without a problem because you only asked for the algorithm.
How it works:
First get the ascii value of the character, let's say the enter "a" now we subtract "a" to get 0. Then we add the shift, let's say, 39. Then we mod by 26 (number of letters in alphabet so we keep our letter as a letter) and we get 13. We add this result of 13 to 'a' to get the final, correct answer of 'n' and we're done.
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
char c;
char encrypt[256]={0}; //don't know how many blanks so must be larger
int choice = 1;
int shift = 13;
if(choice==1) //encrypt
{
gets(encrypt);
for(int done=0;encrypt[done]!='';done++)
{
c=encrypt[done];
if(c!=' ')
{
if(c>='a'&&c<='z') //only code lowercase letters
{
c=((c-'a'+shift)%26)+'a'; //add shift , but if > z must "wrap around"
encrypt[done] = c;
}
else if (c>='A'&&c<='Z')
{
c=((c-'A'+shift)%26)+'A'; //add shift , but if > z must "wrap around"
encrypt[done] = c;
}
else
;// do nothing because it's not a letter
}
}
}
for (int i = 0; i < 256; i++)
{
cout<}
system("PAUSE");
return EXIT_SUCCESS;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.