Hi, I'm trying to write a program that accepts a string (a line, sentence, or phrase) and then the program has to display the contents of that string backward. for example, if I entered "starting out" the program has to convert it to "tuo gnitrats".

This is my code. But I don't know what's wrong with it.

//project #2  backward String (follow page 515,  519)
//write a function that accepts a pointer to a C-string as an argument and displays its contents backwards.

# include <iostream.h>

//function Prototype
void Backward (char *);

void main (void)
{
	char line[201];

	cout << "This program will display the contents of the entered phrase backward.\n";

cout << "Please enter a phrase of no more than 200 characters, followed by a period.\n";
cin.getline(line, 201);

	cout << "The entered string displayed backward: \n";
	cout << Backward (line) << endl;
}

void Backward (char *sentencePtr)
{
	char  *sentencePtr = set;

while (*sentencePtr != '\0' && *sentencePrt > set)
	{
		sentencePtr--;
		cout << *sentencePtr << endl; //or return sentencePtr;
	}
}

can anyone help please?

I don't think you should pass sentence pointer in and then declare it again. Try taking off the 'char' in your backwards function. Also, where the heck is 'set' declared? What is it?

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

>void main (void)
int main()

>char *sentencePtr = set;
What is set? Why are you redeclaring sentencePtr?

>sentencePtr--;
Yea, that'll work. NOT!

Try walking to the end of the string, then back:

char *p = sentencePtr;

while ( *p != '\0' )
  ++p;

while ( p != sentencePtr )
  cout.put ( *--p );

I think you got the declaration backwards for set. It makes more sense the other way around.

Ok
I don't know why I used "set." I'm looking at the examples in my book and just kinda finding my way around. It's trial and error for me, guys. I guess I was trying to give the array a name so that the computer knows how many characters it has and works backward from there...ignore this. I don't know what I was doing.
anyway,
from the suggestions, I recode it:

//project #2  backward String (follow page 515,  519)
//write a function that accepts a pointer to a C-string as an argument and displays its contents backwards.

#include <iostream>
using namespace std;

//function Prototype
void Backward (char *);

int main ()
{
	char line[201];

	cout << "This program will display the contents of the entered phrase backward.\n";

	cout << "Please enter a phrase of no more than 200 characters, followed by a period.\n";
	cin >> line;

	cout << "The entered string displayed backward: \n";
	cout << Backward(line) << endl;
}

void Backward (char *sentencePtr)
{
	
	char *p = sentencePtr;

	while ( *p != '\0' )
	 ++p;

	while ( p != sentencePtr )
	 cout.put ( *--p );
}

and I got the following error:
X:\dtran5.pds\Chapter 10 HWa\Project #2 backward string.cpp(20) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

So what's wrong now?

You're calling backward out of context.
It doesn't return anything (It's void backward(char*)), so you can't get anything out of it. What you need to do is just print out the modified array.

Be careful, the way Narue's written it, it will automatically output the letters, so you can't just leave in your original cout.

Just call backward(line) after you say "The line printed backwards:"

Dont try to print out a void function.
change

cout << Backward(line) << endl;

to

Backward(line);
cout << endl;

Ok, I fixed that. And the problem right now is that the program only shows invert the first word of the sentence and disregards the rest of the sentence.

//project #2  backward String (follow page 515,  519)
//write a function that accepts a pointer to a C-string as an argument and displays its contents backwards.

#include <iostream>
using namespace std;

//function Prototype
void Backward (char *);

int main ()
{
	char line[201];

	cout << "This program will display the contents of the entered phrase backward.\n";

	cout << "Please enter a phrase of no more than 200 characters, followed by a period.\n";
	cin >> line;

	cout << "The entered string displayed backward: \n";
	Backward(line);
	cout << endl;
}

void Backward (char *sentencePtr)
{
	
	char *p = sentencePtr;

	while ( *p != '\0' )
	 ++p;

	while ( p != sentencePtr )
	 cout.put ( *--p );
}

How do I fix that?

>cin >> line;
You had it right the first time, when you were using getline. cin's >> operator stops reading at whitespace, so line only contains the first word.

dang, I'm so stupid, it's not even funny. *LOL* I'm laughing at myself right now.

guess you DO learn something new everyday.

thanks guys,

karen

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.