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"

here is the code:

#include<iostream>
#include<string>
using namespace std;
int length(string word);
void reverse(string word, int l);
int main()
{
	string word;
	cout<<"Enter the word: ";
	cin>>word;
	cout<<"Number of characters --> ";
	int l=length(word);
	cout<<l<<endl;
	reverse(word, l);
	return 0;
}
int length(string word)
{
	int len=0;
	for(int i=0; word[i]!=NULL; i++)
		len++;
	return len;
}
void reverse(string word, int l)
{
	int j=0;
	string rev;
	for(int i=l-1; rev[i]>=0; i--)
		rev[j++]=word[i];
	rev[j]='\0';
	cout<<"The word after reversing --> "<<rev<<endl;
}

Let me know what's wrong ,,, thanks

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.

int length(string word)
{
	int len=0;
	for(int i=0; word[i]!=NULL; i++)
		len++;
	return len;
}

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;

std::string s("Hello, World!");
std::cout << "s is " << s.length() << " characters long" << std::endl;

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.

... the underlying representation of std::string is not necessarily "null terminated"...

Actually, a string is not "null terminated". Period.

Actually, a string is not "null terminated". Period.

Well, the user could manually terminate it with a nul-character, not?

Well, the user could manually terminate it with a nul-character, not?

Only if you want to be obtuse and argumentative. But the definition of string in no way includes 'null terminated' -- in C++ (added to stop the 'well in C' argument :icon_twisted:)

void reverse(string word, int l)
{
	int j=0;
	char rev[l];
	for(int i=l-1; rev[i]>=0; i--)
		rev[j++]=word[i];
	rev[j]='\0';
	cout<<"The word after reversing --> "<<rev<<endl;
}

Now works!

I don't think there's anything in the standard which actively prevents &str[0] from being the same as str.c_str() - AFAIK its something left undefined.

void reverse(string word, int l)
{	
    int j=0;	
    char rev[l];

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).

void reverse(string word, int l)
{	
    int j=0;	
    char rev[l];

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 :

void reverse(string word, int l)
{
	int j=0;
	char* rev = (char*) malloc(l);

    /* Abort if the allocation failed. */
    if (rev == NULL)
        abort();

	for(int i=l-1; i>=0; i--)
		rev[j++]=word[i];

	rev[j]='\0';

	cout<<"The word after reversing --> "<<rev<<endl;

	free(rev);
}

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

I believe you are using a nonstandard language extension, so "works!" either means "it works" or "it doesn't work". As a solution, then, it's not the greatest.

I believe you are using a nonstandard language extension, so "works!" either means "it works" or "it doesn't work". As a solution, then, it's not the greatest.

Just using what i learned.This is my source about char array's..

http://www.cplusplus.com/doc/tutorial/ntcs/

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.