I am working on a problem for school, and I have to write a program that rotates 13 characters. I have the program done, but it rotates into some weird characters. I want to make it look back to 'a' after it reaches 'z' for both uppercase and lowercase. Basically, I want to restrict my options to A-Z and a-z.
Tried a mixture of while statements and if statements, and ended up on with just some if statements. I know they are wrong, but it runs in its current state.
#include <iostream>
using namespace std;
//Function Prototypes
char rot(char c);
int mylen(char c[]);
int main()
{
char in[120], out[120];
int i; // index into in
cout << "Enter text: ";
cin.getline(in,120);
while (strcmp(in, "exit"))
{
for (i = 0; i < mylen(in); i++)
{
out[i] = rot(in[i]);
}
out[i++] = '\0';
cout << out << endl;
cout << endl << "Enter some more text: ";
cin.getline(in,120);
}
return 0;
}
char rot(char c)
{
if (c >= 'a' and c <= 'z')
c = c + 13;
if (c > 'z')
c = c - 26;
else if (c >= 'A' and c <= 'Z')
c = c + 13;
if (c > 'Z')
c = c - 26;
return c;
}
int mylen(char c[])
{
int cnt = 0;
while (c[cnt] != '\0')
cnt++;
return cnt;
}
I am looking just to have it rotate 13 characters, and when someone inputs rotated code, to rotate it again 13 characters.
I suggest you should carefully observe the table of ASCII code to display the character.There are 6 characters between the uppercase letter "A"~"Z" and the lowercase letter "a"~"z"
Here is my code:
if (c >= 'n' && c <= 'z')
{
c = c - 13;
}
else if (c >= 'a' && c <= 'm')
{
c = c + 13;
}
if (c >= 'N' && c<='Z')
{
c = c - 13;
}
else if (c >= 'A' && c <= 'M')
{
c = c+13;
}