C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 22nd, 2006, 02:23 PM
Nefarious22 Nefarious22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 2 Nefarious22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 47 sec
Reputation Power: 0
Send characters from a string in reverse order to output window

I am taking my first programming class and part of our HW is this:

Write, debug and run a C++ program that prompts the user to enter a string; reads the string and sends the characters to the output window in reverse order; thereby reversing the string.

this is what I have :

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string message;

do;
{
cout << "Enter a string:\n";
getline(cin, message);
}
while ( message.length() == 0 );



return 0;
}


I have no idea how to send the characters to the output window in reverse order

Reply With Quote
  #2  
Old April 22nd, 2006, 02:26 PM
ossinator ossinator is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 40 ossinator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 11 m 12 sec
Reputation Power: 3
Send a message via ICQ to ossinator
Just use a for loop starting from the end of the string and counting to the beginning, like this:

for (int i = LEN - 1; i >=0; i --)
{
std::cout << szString[i];
}

How to find the length of the string I leave to you

Reply With Quote
  #3  
Old April 22nd, 2006, 04:11 PM
Nefarious22 Nefarious22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 2 Nefarious22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by ossinator
Just use a for loop starting from the end of the string and counting to the beginning, like this:

for (int i = LEN - 1; i >=0; i --)
{
std::cout << szString[i];
}

How to find the length of the string I leave to you


im still very new to programming and what you have told me helps alot I still need more help, The book my school uses is not answering my questions.

I can find the length, but like I dont know why you put the " std:: " and just in general i dont know how to finish this program properly

Reply With Quote
  #4  
Old April 22nd, 2006, 08:33 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main ()
{
    string choose;
    cout<<"Write Something\n";
    
    getline(cin,choose);
    
    for(int i = choose.length()-1;i>=0;i--)
    /*The for statement has three arguments in order to accomplish this task
    1] int i = choose.length()-1 gets the length of the string and subtracts
       the null character so you dont get a blank space in the return
       try it both ways to see what i mean
    2] i greater than zero to terminate loop is self explanatory.
    3] i-- counts backwards to give the desired affect.
    */
    {
            cout<<choose[i];/*since a string is basically an array of characters
            you can use them as such, so i is now the length of your string and 
            every time the loop processes it subtracts one */
            }
            
            
	system("PAUSE");
	return 0;
}


The std:: is used if you dont use "using namespace std" which just makes the std a namespace so you can use cout and cin instead of typing std::cout every time. Looks like you have most of the stuff down just not your loops, might want to look into practicing a little more with those. Hope this explanation helps ya a little. Most people dont like responding to homework but it looks like you atleast made an attempt.
__________________
---Official Member Of The Itsacon Fan Club---
Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Send characters from a string in reverse order to output window


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT