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