As a novice in c++, I try to use it could reverse a string with this
class Solution {
public:
void reverses(string s,int begin,int end){
char c;
for(int i=begin,j=end-1;i<j;i++,j--){
c=s[i];
std::cout<<s[i]<<std::endl;
s[i]=s[j];
s[j]=c;
std::cout<<s[j]<<std::endl;
}
}
string reverseLeftWords(string s, int n) {
reverses(s,0,n);
return s;
}
};
But it give me the original string. But when i use it in *char `
void Reverse(char *s,int n){
for(int i=0,j=n-1;i<j;i++,j--){
char c=s[i];
s[i]=s[j];
s[j]=c;
}
}
int main()
{
char s[]="hello";
Reverse(s,5);
cout<<s<<endl;
return 0;
}
it out put olleh, what's different between them?
std::swap. You should also be careful with const char* string literals. I would expect your code to crash on some systems.