-2

For example, if I introduce hello, the program through the ASCII table will return the same but character 7 letters forward: 'hello' -> 'olssu'. Thanks in advance. I have tried this but it obviously doesn't work:

  

#include <iostream>
using namespace std;


int main()
{
    int asciiValue=65;
    string pass;
    cout<<"introduce la contraseña"<<endl;
    cin>>pass;
    char character = char(asciiValue+7);
    for(int a=0;a<pass.length();a++){
        
        cout<<character;
        cout<<pass[a];
    }

    return 0;
}
Valerii Boldakov
  • 1,751
  • 9
  • 19

1 Answers1

0

In your example, 'hello' would become 'olssv' moving ahead by 7 positions. The code for achieving this would look like this:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string inputString;
    cin>>inputString;

    // iterate through the string
    for(int i=0; i< inputString.length(); i++) {
        
        // for each character, get its ASCII value
        // in C++, if you simply parse a character to an int
        // you will get the ASCII value
        int asciiValue = int(inputString[i]);

        // add 7 to the ascii value and print it as a char
        cout<<char(asciiValue + 7);
    }

    return 0;
}
Sidharth Ramesh
  • 646
  • 2
  • 6
  • 21