loopy

I don't know what to do here and no one seems to have an answer that I can use. I guess I can't use the function like this but isn't there some other way to initialize i to the value of word.length()? I've got this due at midnight and I'm new with this stuff so try to keep the answers simple please. Thanks!

for(i=word.length();i>=0;i--)
Have you declared "i" yet (I'm assuming that it's an interger)?
Yes, i is an integer, word is a string.
I guess I can't use the function like this
¿What do you want to do? ¿why isn't that working?
Well I'll go ahead and post the assignment I guess but I have to do it the way he's asking even though I realize there are easier ways of reversing words.

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


Here's what I want to do but it doesn't work: (If I put i=5 in place of the toReverse.length(), for example, the program works fine, but only if the word is 5 characters long)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

string reverse(string toReverse);

int main()
{
	string toReverse;
	cout << "Enter a string: ";
	cin >> toReverse;
	cout << "Reverse = " << reverse(toReverse);
	return 0;
}

string reverse(string toReverse){
	string bob;
	int i;
	for(i=toReverse.length();i>=0;i--){
		bob+=toReverse.at(i);
	}
	return bob;
}


I really appreciate the help
length() is out of bounds. Remember that .at() goes from 0 to length() -1
Ahhh ok that was stupid of me. But thank you!
Topic archived. No new replies allowed.