0

If I input ''ABCD'' I want the output to be ''ZABC''. If I input ''hello world'' the output has to be ''gdkkn vnqkc''.

I did this:

cout << "Enter text" << endl;
cin >> input;`

result_string = ConvertString(input);

cout << endl << "The result is " << endl << result_string << endl;

But I don't know how to define ConvertString.

Thank you for the help. :)

owacoder
  • 4,815
  • 20
  • 47
  • What have you tried so far? SO is not a free coding service, as is often pointed out. – owacoder Oct 05 '15 at 03:15
  • that's all I did so far since i have no idea how it can be done, i don't understand how to convert every character in a string without using some complicated stuff... (i just started learning and all i know is the very basics). – Mohan Nassif Oct 05 '15 at 03:20
  • I looked everywhere on Google and YouTube and saw a lot of similar stuff like Converting a series of letters by numbers or converting every letter in a input by another letter, but no one seems to have asked this one before. And i only ask for a little tip, I'm not asking someone to program this for me i'm just trying to understand C++ – Mohan Nassif Oct 05 '15 at 03:22
  • See [this question](http://stackoverflow.com/questions/9438209/for-every-character-in-string) for some hints. – owacoder Oct 05 '15 at 03:25
  • Thank you for the hint @owacoder – Mohan Nassif Oct 05 '15 at 03:43

2 Answers2

1

You would have to access every individual char in the string. String allows you to access memory randomly, so you can do so with a for loop.

string input;
cout << "Enter text" << endl;
cin >> input;

//traverse the string
for (int i = 0;i < input.size(); i++)
{
//check for A or a and move it to Z or z respectively
if(input[i] == 65 || input[i] == 97)
input[i] += 26;
//change the value to minus one 
input[i]--;
}

cout << endl << "The result is " << endl << input << endl;
return 0;
Felipe Centeno
  • 2,911
  • 1
  • 21
  • 39
1

ever heard of caesar cipher?

#include<iostream>
#include<string>
using std::endl;
using std::cout;
using std::cin;


int main(){
    std::string input;
    int count=0, length, n;

    cout<<"enter your message to encrypt:\n";
    getline(cin,input);
    cout<<"enter number of shifts in the alphabet:\n";
    cin>>n;

    length=input.length();//check for phrase's length

    for(count=0;count<length;count++){
        if(std::isalpha(input[count])){
            //lower case
            input[count]=tolower(input[count]);
            for(int i=0;i<n;i++){
                //if alphbet reaches the end, it starts from the beginning again
                if(input[count]=='z')
                    input[count]='a';
                else
                    input[count]++;    
            }    
        }    
    }

    cout<<"your encrypted message: \n"<<input<<endl;

    return 0;    
}
夢のの夢
  • 5,054
  • 7
  • 33
  • 63
  • Great mentioning the *Caesar Cipher* term, but you're "incrementing" rather than "decrementing" the ASCII values, and the change to lowercase may or may not be acceptable. – Tony Delroy Oct 05 '15 at 04:04