#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
main() {
char hoi[500];
int i; // i becomes the size of vector kip
cout << "Hello!" << endl << "type something: ";
cin.getline(hoi,500);
vector<char> kip (hoi, hoi + sizeof(hoi) / sizeof(int) ); // standard coding
for (i=kip.size(); i >= 0; i--) // as long as i isn't less than 0, the vector is printed.
cout << kip[i];
cin.get();
return 0;
}
and what I get is this:
C:\CPPs\hoi>hoi
Hello!
type something: damn it
¶ ☻ å*@ ↕²t Aí@ ↕²╚Zz■░ ↕²< AíÇ @╪☼ ↕²╠ ↕²¿ → @;H z Z ¶N≡ A{¼ ↕■░ A¥( å1É ☻ ↕²á
@╖ⁿ ti nmad
So what happens is that empty chars are getting stored in the vector, and the vector prints them as strange chars.
You can use strlen from <cstring> to give you the length up to (but not including) the null, which marks the end. andm you shouldn't be dividing by sizeof(int), hoi + strlen(hoi) will point to the last letter.
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
main() {
char hoi[500];
int i; // i becomes the size of vector kip
cout << "Hello!" << endl << "type something: ";
cin.getline(hoi,500);
vector<char> kip (hoi, hoi + strlen(hoi)); // standard coding
for (i=kip.size()-1; i >= 0; i--) // as long as i isn't less than 0, the vector is printed.
cout << kip[i];
cin.get();
return 0;
}