473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Having problem with this program

1
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <iterator>
  5. #include <algorithm>
  6. #include <functional>
  7. using namespace std;
  8.  
  9.  
  10. void reverse(string& s)
  11. {
  12.  int i = 0, j = s.length() - 1;
  13.  while(i < j)
  14.  {
  15.   swap(s[i++], s[j--]);
  16.  }
  17. }
  18.  
  19. void main()
  20. {
  21.  
  22.  
  23. vector<string> theWords;
  24. theWords.reserve(100);
  25.  
  26.  
  27. istream_iterator<string> stringReader(cin);
  28. istream_iterator<string> stringReaderEOF;
  29.  
  30.  
  31. while(stringReader != stringReaderEOF)
  32. {
  33. theWords.push_back(*stringReader++);
  34. }
  35.  
  36.  
  37. cout << "The words read in are:\n";
  38.  
  39. copy(theWords.begin(), theWords.end(),ostream_iterator<string>(cout," ") );
  40.  
  41. vector<string> aCopy(theWords);
  42. reverse;
  43.  
  44. for_each(theWords.begin(), theWords.end(), aCopy );
  45. cout << "\nAfter reversing all the strings in the copy:\n";
  46. copy( aCopy.begin(), aCopy.end(),ostream_iterator<string>(cout,"\n " ));
  47.  
  48. sort( aCopy.begin(), aCopy.end(),greater<string>());
  49. cout << "\nAfter sorting the strings in descending order:\n";
  50.  
  51. copy(aCopy.begin(), aCopy.end(),ostream_iterator<string>(cout,"\n " ) );
  52. }

All i want to do is...
reverse the string and print the answers like this

the rain in spain falls mainly in the plain
^Z
^Z
The words read in are:
the
rain
in
spain
falls
mainly
in
the
plain
After reversing all the strings in the copy:
eht
niar
ni
niaps
sllaf
ylniam
ni
eht
nialp
After sorting the strings in descending order:
the
the
spain
rain
plain
mainly
in
in
falls
Press any key to continue
Apr 6 '09 #1
2 1428
donbock
2,426 Expert 2GB
You didn't tell us what's wrong. It would be a lot easier for everybody if you simply stated how the program disappoints you (compiler error, run-time error, wrong output, etc.) rather than force us to guess at the problem.
Apr 6 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
I think you are making this way too hard.

If you have the words in a vector<string>, then you can reverse iterate by using a vector<string>::reverse_iterator and running your copy from rbegin() to rend().

Expand|Select|Wrap|Line Numbers
  1. copy(theWords.rbegin(), theWords.rend(),ostream_iterator<string>(cout," ") );
It looks to me that your whole program is reduced to this one line of code.
Apr 7 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Curious Angel | last post by:
In my eternal search to locate a utility that would allow me to export PAGEMAKER documents (6.0, in this case) —> to HTML I stumbled upon this program, and was thrilled. I can do HTML, don't get...
0
by: happy | last post by:
That request of having sorting program for such setup programs was not a request to an assignment but to have good ideas about sorting program .. #include <stdio.h> #include <conio.h> struct...
13
by: Jacek Dziedzic | last post by:
Hi! <OT, background> I am in a situation where I use two compilers from different vendors to compile my program. It seems that recently, due to a misconfiguration, library conflict or my...
17
by: Chad | last post by:
I'm want static char *output; to hold the modified string "tel chad" However, when I debug it, static char *output holds the ascii value of the strng, and not the string itself. Here is...
3
by: i80and | last post by:
I'm working on a basic web spider, and I'm having problems with the urlparser. This is the effected function: ------------------------------ def FindLinks(Website): WebsiteLen = len(Website)+1...
6
by: 3than7 | last post by:
I am writing an application to solve Pythagorean Theorum Problems. This is on my own time, i am using a book to learn c++, and after doing a fahrenheit to celsuis program from that book, i wanted...
18
by: sam_cit | last post by:
Hi Everyone, int main() { printf("not included stdio.h"); } Yes, i haven't included stdio.h and my compiler would generate a warning and would assume that it would return a int, my question...
0
by: pete | last post by:
Greetings all, I am going to attempt to describe the issue that I am having in full. I apologize ahead of time that this may be a long posting, however, I have found no other solution to my...
9
by: raashid bhatt | last post by:
does having more variables increases the size of program.
2
by: cmb3587 | last post by:
I am having a problem with the validation of the account number and password. The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.