You might find such a thing here, perhaps in the snippets, maybe a previous forum thread. So a search might be helpful to you.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
These are separate, independent tasks, so tackle them separately and independently. Have two functions, "Flip" and "Reverse". Both involve swapping values, so you'll need to write a "Swap" function.
void Swap (char& char1, char& char2)
{
// put value of char1 into char2 and value of char2 into char1
}
This is a helper function for both "Flip" and "Reverse". Both "Flip" and "Reverse" need to know whether the string is an even or odd number of characters. In each function, if the string is, say 11 characters long, you'll have 11 / 2 = 5 swaps total. If it's 10 characters long, you'll have 10 / 2 = 5 swaps total. Let's say you have a string that's 11 characters long : "abcdefghijk". For Reverse:Swap indexes 0 and 10.
Swap indexes 1 and 9.
Swap indexes 2 and 8.
Swap indexes 3 and 7.
Swap indexes 4 and 6.
Index 5 stays the same.
For Flip:
Swap indexes 0 and 1.
Swap indexes 2 and 3.
Swap indexes 4 and 5.
Swap indexes 6 and 7.
Swap indexes 8 and 9.
Index 10 stays the same.
See the pattern? Write "Swap", set up a for-loop in each function, and do the appropriate swaps based on the pattern and the for-loop control variable.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
I do not like to approach a took to this problem before. I messed up sorry. I am going to start off by posting what I have so far.
#include "stdafx.h"
#include <iostream>
using namespace std;
char teststr[] = "Have a nice day!!";
int _tmain(int argc, _TCHAR* argv[])
{
cout << "The unencrypted string is: " << teststr << endl;
cout << "What type of encryption would you like to use?" << endl;
cout << "(r) Reverse" << endl << "(f) Flipper" << endl;
if(r)
{
cout << "Encrypted your string is: " << reverse << endl;
}
return 0;
}
void reverse(char unencrypted[], char encrypted[]);
Okay as you see I have the function at the bottom, I just need help putting it into action. There will be another form of encryption later emplimted, that's why the user is to chose r or f. I also need help to get what they input to....actually do something I need to declare the variables r and f somehow but I don't know how
I don't know if this pseudo-code or real code, but if it's real code, line 14 is going to cause you problems. A function call needs parameters. Based on your function prototype, you need at least 3 variables:
char option; // user input of either 'r' or 'f'
char encrypted[200]; // 200 is some arbitrarily large number
char unencrypted[200]; // 200 is some arbitrarily large number
You need to prompt the user to enter a string and read it into the unencrypted[] string. you need to prompt the user to enter 'r' or 'f' and read it into option. Then do this:
if (option == 'f')
{
flip (unencrypted, encrypted);
}
else if (option == 'r')
{
reverse (unencrypted, encrypted);
}
As a side note, I've never really worked with TCHAR. Is that unicode or something? Not sure if it's compatible with the plain old char type or not. If not, change types accordingly so they work.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Tell me in psuedo code what each of these function below needs to do in order to achieve its purpose
void reverse(char unencrypted[], char encrypted[]);
void flip(char unencrypted[], char encrypted[]);
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608