Reverse a string

Hey everyone,,,
tomorrow is my c++ final,,, and am having a problem with a simple code,,, it seems logical but i do not know why it is not working,,,
I wrote two function one that get the length of the string and another to reverse the character in the string,,,, the first one works but the second one give an error when I run the code after the first part is excuted
the error says: "string subscript out of range"

I sometimes have problems using the [] operator for STL strings in the fashion you are. I believe this has to do with the default size of an STL string and that using [] doesn't automatically increase size of an STL string when needed whereas other operators like + and += do cause automatic extension of the size of the string when needed. I would try using the += operator rather than assignment.

This function is completely unnecessary - it could also lead to undefined behaviour since the underlying representation of std::string is not necessarily "null terminated", which means that you could quite easily crash your program or do other strange things with this construct word[i]!=NULL However, it shouldn't matter, since std::string already has a "length" function;

Even if your length function appears to work, since a std::string is not null terminated, your length function may overrun if the first byte after the end of your string happens to be some other junk value (It may or may not be memory owned by the string - but don't leave it to chance - you've got a 50/50 chance of something going wrong). The only way to guarantee access to a null terminated string is with the .c_str() function - at which point you may aswell simply use strlen instead.

Since l is not a const int I would expect this to crash or cause an error. Using dynamic memory to declare space for rev using l + 1 as the amount of memory to declare makes sense (assuming l is the length of word and not the length of word + 1).

Since l is not a const int I would expect this to crash or cause an error. Using dynamic memory to declare space for rev using l + 1 as the amount of memory to declare makes sense (assuming l is the length of word and not the length of word + 1).

I'm just declaring a char array like a local variable i don't think we need to do :

Nowhere in that link do I see where it says that a nonconstant int can be used when declaring memory for an array using static memory. It does use several different methods for getting the memory statically, including letthing the compiler do it when using string literals and using constant ints like 80 or 44 or 17. If your compiler lets you use nonconstant ints to declare arrays statically, so be it. But, at this time at least, such practice isn't standard.