I am attempting to make a program that would take a phrase and the flip it backwards like this:

char teststr[] = “Have a nice day!!”;

and change it to !!yad ecin a evaH

Function?

void reverse(char unencrypted[], char encrypted[]);

Recommended Answers

All 9 Replies

You might find such a thing here, perhaps in the snippets, maybe a previous forum thread. So a search might be helpful to you.

This code snippet may help you.
http://www.daniweb.com/code/snippet217345.html
Its written in C but you should be able to see what us being done. You should always search the forums before you have start a new thread. You may just find your answer there.

This code snippet may help you.
http://www.daniweb.com/code/snippet217345.html
Its written in C but you should be able to see what us being done. You should always search the forums before you have start a new thread. You may just find your answer there.

Thanks for that link. Okay so just through in a reverse, well what if i wanted to flip the pairs? Like 12345 to 21435

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

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:

  1. Swap indexes 0 and 10.
  2. Swap indexes 1 and 9.
  3. Swap indexes 2 and 8.
  4. Swap indexes 3 and 7.
  5. Swap indexes 4 and 6.
  6. Index 5 stays the same.

For Flip:

  1. Swap indexes 0 and 1.
  2. Swap indexes 2 and 3.
  3. Swap indexes 4 and 5.
  4. Swap indexes 6 and 7.
  5. Swap indexes 8 and 9.
  6. 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.

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.

I really appreciate all that you put into that post. That's alot of valuable information that will help me alot. I am going to work on my project for awhile and I will probably have more questions later.

Thank You! :)

oh and that Tchar thing is just something the microsoft compiler troughs in by default I really don't know what it means.

Now I am stuck again. I have most of the program done now I am just missing the functions. What I need help with is how to go about writing them, I have never really understood functions.

#include "stdafx.h"
#include <iostream>
using namespace std;

void reverse(char unencrypted[], char encrypted[]); // This is the first functions declration

void flip(char unencrypted[], char encrypted[]); // This is the second functions declaration 

int _tmain(int argc, _TCHAR* argv[])
{

char option;
char encrypted[35];
char unencrypted[35];
char teststr[] = "the quick brown fox jumps over the lazy dog";
	
	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;
	cin >> option;
	if (option == 'f')
	{
		flip (unencrypted, encrypted);
		cout << "Encrypted your string is: " << endl; 
	}
	else if (option == 'r')
	{
		reverse (unencrypted, encrypted);
		cout << "Encrypted your string is: " << // how do I call the functions here? endl;
	}

	return 0;
}

// how do i define the functions down here?

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[]);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.