944,085 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10127
  • C++ RSS
Jul 25th, 2005
0

backward string

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 25th, 2005
0

Re: backward string

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?
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 25th, 2005
0

Re: backward string

># 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:
C++ Syntax (Toggle Plain Text)
  1. char *p = sentencePtr;
  2.  
  3. while ( *p != '\0' )
  4. ++p;
  5.  
  6. while ( p != sentencePtr )
  7. cout.put ( *--p );
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 25th, 2005
0

Re: backward string

I think you got the declaration backwards for set. It makes more sense the other way around.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 25th, 2005
0

Re: backward string

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:

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 25th, 2005
0

Re: backward string

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:"
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 25th, 2005
0

Re: backward string

Dont try to print out a void function.
change
C++ Syntax (Toggle Plain Text)
  1. cout << Backward(line) << endl;
to
[CODE]
Backward(line);
cout << endl;
[CODE]
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005
Jul 25th, 2005
0

Re: backward string

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.

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 25th, 2005
0

Re: backward string

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 25th, 2005
0

Re: backward string

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
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problems casting a const char* to char*
Next Thread in C++ Forum Timeline: Beginner with C++ and goofy run time problem with DBL Linked List





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC