for(i=word.length();i>=0;i--)
I guess I can't use the function like this |
Write a function named reverse that takes a string parameter and returns a new string that is the reversed version of the original string. For example, if the parameter string is “smiley”, reverse should return the string “yelims”. To reverse a string, create an empty string and then start at the last character in the original string. Use the concatenation operator, +, to successively concatenate characters from the end of the original string to the new string. For example, with the string “smiley”, you would start with the last character ‘y’ and concatenate it to your new string. Then you would concatenate ‘e’, ‘l’, ‘i’, and so on until you reach the beginning of the string. To locate the last character in the string and to access the individual characters in a string you will need to use the following two string functions: 1. char at(index i): returns the character at location i. The indices start at 0, so at(0) would return ‘s’ for “smiley”, at(1) would return ‘m’, and at(5) would return ‘y’. 2. int length(): returns the number of characters in the string. For the string “smiley”, length would return 6. These functions are special functions in that you call them by prefacing them with the name of the variable containing the string, then a period, ‘.’, and finally the function name. For example, if “smiley” is contained in the string variable s, then s.at(0) returns ‘s’ and s.length() returns 6. Once you have written reverse, test it by writing a main function that prompts the user for a string, calls reverse with this string as a parameter, and prints out the reversed string. Here is some sample output: enter a string: racecar reversed string = racecar |
|
|