954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

backward string

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?

karen_CSE
Light Poster
47 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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?

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

># include
#include
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 );
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

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?

karen_CSE
Light Poster
47 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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:"

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

Dont try to print out a void function.
change

cout << Backward(line) << endl;

to[CODE]
Backward(line);
cout << endl;
[CODE]

zyruz
Junior Poster in Training
60 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
 

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?

karen_CSE
Light Poster
47 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

karen_CSE
Light Poster
47 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You