// this program reverses the order of letters in a word
#include <iostream>
#include <string>
#include <stdio.h>
usingnamespace std;
int Reverse(string a);
int main()
{
string word;
cout << "Enter a word you would like to reverse: ";
getline(cin, word);
word = Reverse(word);
cout << "\n\nThe new word is: " << word << endl;
}
int Reverse(string a)
{
int i, newword;
i = a.length();
for (int b=i; b>0; b--)
{
newword += a.at(i);
}
return newword;
}
.at() indexing is like subscript (array[0]) and as such will be 1-off from counting index (first letter in string is .at(0), etc) and the last "valid" index will be string.at(string.length() - 1); Your i = string.length() so it is 1 over the last character in the string (and thus out of range).