Tuesday, July 28, 2009

Writing a C++ program to display letters backwards?

I'm going through a C++ book before I return to school in the fall and have come across a problem that is giving me issues. Basically, I am supposed to initialize a pointer-based string variable to all 26-letters in the alphabet in upper case. So it should be "ABCDEFGHIJKLMNOPQRSTUWXYZ". Then I am supposed to declare a character array based string variable to store up to 26 characters to be entered from the user. I should then print what the user entered, and then print the upper case alphabet backwards, and then what the user entered backwards. I can only use arrays and pointer-based arrays, as that's all I know right now.





Can someone give me a hand with this? Should output:





The first string is:


"ABCDEFGHIJKLMNOPQRSTUWXYZ"


Enter the second string up to 26 characters:


abcdefghijklmno


You have entered:


"abcdefghijklmnopqrstuvwxyz"


The first alphabet backwards is as follows :


ZYXWUTSRQPONMLKJIHGFEDCBA





The second alphabet backwards is as follows :


zyxwvutsrqponmlkjihgfedcba





Thanks!

Writing a C++ program to display letters backwards?
As an example of what mapaghimagsik is advising, I provide the following example. Doesn't get too much sweeter than this but sadly so few people seem to make it through all the C junk while learning C++ that they never get to a lot of the good stuff.





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





int main()


{


string s("abcdefghijklmnopqrstuvwxyz");





string::reverse_iterator rit = s.rbegin();





while (rit != s.rend())


{


cout %26lt;%26lt; *rit++;


}





cout %26lt;%26lt; endl;


}
Reply:Use a for loop to print a string backwards. Initialize it to the length of the string and terminate on 0.





The alphabet is a literal string.
Reply:check out: http://www.planet-source-code.com
Reply:You can get a reverse_iterator on a string. Use that to move backwards through the arrays.
Reply:Ascii numbers for the alphabets are starting from 65-90(A-Z)


then use the ascii numbers to print the alphabets in the reverse order, if ur program is only with alphabets. If u want to reverse the string take the string length and read from last to first number to print the alphabets in the reverse order.

survey monkey

No comments:

Post a Comment