Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
A program is to be written to input a a message in string and then encrypt the message.

output "Enter message"
Input MyMessage
EncryptString= ""

For i- 1 to CHARACTERCOUNT (mymessage)
NextNum - ASC(ONECHAR(MYMessage,i) + 3)
EncryptString - EncryptString & CHR(NextNum)
Endfor
Output encryptstring

ONECHAR("Barcelona", 3) returns 'r'
CHARACTERCOUNT ("South Africa") returns 12


What I have tried:

Please help me write this algorithm in C++
Mystring=Input ('key in string')

Stringtotal=0

for i in range (0,len(Mystring)):

NextNum = ord( Mystring[i])

StringTotal= StringTotal + NextNum

Print(MyString, stringTotal)


Have tried this in python help me convert it to C++
Posted
Updated 22-Aug-20 4:55am
v4
Comments
Jochen Arndt 22-Jun-18 3:08am    
You do know Basic and C/C++?
If so, it is no problem to do it yourself.

If not, what is your task?
Convert that specific Basic code, but you don't know Basic?

Or do you have some kind of C++ homework, found that Basic code, and thought that it is doing what the assignment wants you to do?

Anyway, it is a ROT3 letter substitution cipher without handling roll-over. That means the code of each character is incremented by 3.
Member 13795308 22-Jun-18 3:20am    
Have recently started c++ , gone through basic. Assignment was to convert it into python code which I have done but I want to experiment it in c++. I am quite new to it, so I could only do input and output but got stuck in the loop no idea how to write it

Quote:
Have recently started c++ , gone through basic. Assignment was to convert it into python code which I have done but I want to experiment it in c++. I am quite new to it, so I could only do input and output but got stuck in the loop no idea how to write it

Even your Python code is missing the +3 part.

But in C/C++ it is much simpler. If you have C string (char array) or a std::string you can access the array by index / [] operator:
C++
input[i] += 3;
Note that the above code snippet assumes that output has been initialised with a copy of the input string.

Alternatively use a Range-based for loop (since C++11) - cppreference.com[^] :
C++
// input is of type std::string
std::string output;
for (auto c : input)
    output += c + 3;
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900