Cho em hỏi bài viết hàm reverse đảo ngược xâu em viết như này sai ở đâu ạ??
Chạy thì nó ra là “olllo” chứ không phải “olleh” như mong muốn ạ!
#include <iostream>
#include <cstring>
using namespace std;
void reverse (char *s) {
int n = strlen(s);
int i = 0;
char *tmp = s;
while (n != 0) {
*(s+i) = *(tmp + n - 1);
n--;
i++;
}
}
int main() {
char s[] = "hello";
reverse(s);
cout << s;
return 0;
}