Many times we need to reverse something. It could be anything from our
point of view to our tracks. If we are landing in a jet airplane, we
might need to reverse the thrust without slapping the jet engines in
reverse (not recommended).
Design a program called reverser.cpp which should accept a string from the user and then return the string in reverse order. It might be interesting to enhance this a bit by reversing the string, converting it all to upper case and keeping only the alphabetic characters. In order to get at the individual pieces of a string, you will need to understand a little bit about arrays and loops. A string is actually an array of single characters. For example:
Design a program called reverser.cpp which should accept a string from the user and then return the string in reverse order. It might be interesting to enhance this a bit by reversing the string, converting it all to upper case and keeping only the alphabetic characters. In order to get at the individual pieces of a string, you will need to understand a little bit about arrays and loops. A string is actually an array of single characters. For example:
string abe = "Lincoln";abe is actually an array. abe[0] is 'L', abe[1] is 'i', ... and abe[6] is 'n'. There is also a function or method for a string which tells its length and that would be abe.length() which is 7 in this case.
string auto = "Cadillac"; cout << auto.length() << endl; // this should print 8 cout << auto[2] << endl; // this should print out 'd' (remember we start at zero)

0 comments:
Post a Comment