char ascii and conversion

Hi, i need to change every letter from the word that was entered into its 3rd "neighbour" in ABC. E.G., word "GRAMMAR" would be executed as "JUDPPDU". The problem is that in some places of an executed word i get characters instead of a latin letter. I have to use only simple equations, basically, no constant functions for conversion or etc., because im only a beginner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int a;
    char c;
    ifstream fd ("text.txt");
    ofstream fr ("results.txt");
    string s;
    fd.ignore();
    getline(fd,s);
    a = s.length();
    for (int i = 0 ; i <= a; i++){
        c = (s[i]+3);
        if(s[i] == ' '){
            fr << ' ';
        }
        else
        fr << c;
    }
    return 0;
}
Last edited on
for (int i = 0; i < a; i++)
It still gives me some symbols instead of letters if i type different words.
If the input is in the range "A" through "W" then the result will be an uppercase letter.

What input are you using (sample contents of "text.txt")?
Give me your examples.
If you have Z letter of course you will have ] symbol by ASCII code.
Last edited on
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
27
28
29
30
31
32
33
34
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int a;
    char c;
    ifstream fd ("text.txt");
    ofstream fr ("results.txt");
    string s;
    fd.ignore();
    getline(fd,s);
    a = s.length();
    for (int i = 0 ; i < a; i++){
        c = (s[i]+3);
        if(s[i] == ' '){
            fr << ' ';
        }
        else if (s[i] == 'X'){
            fr << 'A';
            }
        else if (s[i] == 'Y'){
            fr << 'B';
            }
        else if (s[i] == 'Z'){
            fr << 'C';
        }
        else{
        fr << c;
        }
    }
    return 0;
}
I made this but it doesnt print the first letter of the word if it is "A". Also I think the "else if's" can be written differently and shorter
Last edited on
closed account (48T7M4Gy)
As a convenience convert to upper case

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
27
28
29
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    size_t len;
    char c;
    std::string str;
    
    std::cout << "Please, enter your text: ";
    std::getline (std::cin,str);
    
    len = str.length();
    
    for (size_t i = 0 ; i < len; i++)
    {
        c = toupper(str[i]);
        
        if(isalnum(c))
            c = 'A' + ( c - 'A' + 3) % 26;
        else
            c = str[i];
        
        std::cout << c;
    }
    std::cout << '\n';
    return 0;
}


Please, enter your text: aBC a123/m l?'B
DEF D456/P O?'E
Program ended with exit code: 0


Please, enter your text: GRAMMAR
JUDPPDU
Program ended with exit code: 0
Last edited on
If you want to read more about what Kemort used google "Caesar Cipher", Offsetting by 3 is like having a cipher of each letter + 3

The algorithm a caesar cipher is:
Ciphertext = (Plaintext + Key) % 26
The problem with coding it is that: It assumes character values 0-25, a-z.

Let c = plaintext
c = 'G';
c = (c + 3) % 26
We will receive a result of 22, Which is not 'J'
Well okay, Lets add the Char value 'A' to try to get a char value.

c = 'A' + (c + 3) % 26
65 + (71 + 3) % 26
65 + 22 //74 mod 26 is 22
We will receive a result of 87, Which is 'W' not 'J'
Darn okay. Well J is only a value of 9 From A. So lets get the parenthesis to equal 9.
To do that we see that we ask what value can be subtracted from 71 to equal 6. (We already know we have to add 3)
71 - 65(the value of 'A') will give us 6... plus the 3 we already need to shift... 9!

c = 'A' + (c - 'A' + 3) % 26
65 + (71 - 65 + 3) % 26
65 + (6 + 3) % 26
65 + 9 % 26 //9 modulo 26 is 9.
=74
74 is the value of 'J'!

An alternative is to set A-Z to 0-25, This can be easily accomplished by throwing them in a string and using Findfirstof to find the position value of the letter, and then using a caesar cipher.

ABCDEFGHIJ
0123456789

c = (c + 3) % 26
c = (6 + 3) % 26
c = 9 % 26
c = 9
Now compare that value back to the string
G = J

This way makes it easier to comprehend, however im pretty sure its less efficient. (Im still a n00b though so maybe its acceptable)
Last edited on
Topic archived. No new replies allowed.