backward string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

backward string

 
0
  #1
Jul 25th, 2005
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.

  1.  
  2. //project #2 backward String (follow page 515, 519)
  3. //write a function that accepts a pointer to a C-string as an argument and displays its contents backwards.
  4.  
  5. # include <iostream.h>
  6.  
  7. //function Prototype
  8. void Backward (char *);
  9.  
  10. void main (void)
  11. {
  12. char line[201];
  13.  
  14. cout << "This program will display the contents of the entered phrase backward.\n";
  15.  
  16. cout << "Please enter a phrase of no more than 200 characters, followed by a period.\n";
  17. cin.getline(line, 201);
  18.  
  19. cout << "The entered string displayed backward: \n";
  20. cout << Backward (line) << endl;
  21. }
  22.  
  23. void Backward (char *sentencePtr)
  24. {
  25. char *sentencePtr = set;
  26.  
  27. while (*sentencePtr != '\0' && *sentencePrt > set)
  28. {
  29. sentencePtr--;
  30. cout << *sentencePtr << endl; //or return sentencePtr;
  31. }
  32. }

can anyone help please?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: backward string

 
0
  #2
Jul 25th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: backward string

 
0
  #3
Jul 25th, 2005
># 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:
  1. char *p = sentencePtr;
  2.  
  3. while ( *p != '\0' )
  4. ++p;
  5.  
  6. while ( p != sentencePtr )
  7. cout.put ( *--p );
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: backward string

 
0
  #4
Jul 25th, 2005
I think you got the declaration backwards for set. It makes more sense the other way around.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: backward string

 
0
  #5
Jul 25th, 2005
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:

  1.  
  2. //project #2 backward String (follow page 515, 519)
  3. //write a function that accepts a pointer to a C-string as an argument and displays its contents backwards.
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. //function Prototype
  9. void Backward (char *);
  10.  
  11. int main ()
  12. {
  13. char line[201];
  14.  
  15. cout << "This program will display the contents of the entered phrase backward.\n";
  16.  
  17. cout << "Please enter a phrase of no more than 200 characters, followed by a period.\n";
  18. cin >> line;
  19.  
  20. cout << "The entered string displayed backward: \n";
  21. cout << Backward(line) << endl;
  22. }
  23.  
  24. void Backward (char *sentencePtr)
  25. {
  26.  
  27. char *p = sentencePtr;
  28.  
  29. while ( *p != '\0' )
  30. ++p;
  31.  
  32. while ( p != sentencePtr )
  33. cout.put ( *--p );
  34. }

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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: backward string

 
0
  #6
Jul 25th, 2005
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:"
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: backward string

 
0
  #7
Jul 25th, 2005
Dont try to print out a void function.
change
  1. cout << Backward(line) << endl;
to
[CODE]
Backward(line);
cout << endl;
[CODE]
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: backward string

 
0
  #8
Jul 25th, 2005
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.

  1.  
  2. //project #2 backward String (follow page 515, 519)
  3. //write a function that accepts a pointer to a C-string as an argument and displays its contents backwards.
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. //function Prototype
  9. void Backward (char *);
  10.  
  11. int main ()
  12. {
  13. char line[201];
  14.  
  15. cout << "This program will display the contents of the entered phrase backward.\n";
  16.  
  17. cout << "Please enter a phrase of no more than 200 characters, followed by a period.\n";
  18. cin >> line;
  19.  
  20. cout << "The entered string displayed backward: \n";
  21. Backward(line);
  22. cout << endl;
  23. }
  24.  
  25. void Backward (char *sentencePtr)
  26. {
  27.  
  28. char *p = sentencePtr;
  29.  
  30. while ( *p != '\0' )
  31. ++p;
  32.  
  33. while ( p != sentencePtr )
  34. cout.put ( *--p );
  35. }

How do I fix that?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: backward string

 
0
  #9
Jul 25th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: backward string

 
0
  #10
Jul 25th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC