c++ - Dynamically increase and decrease char arrays? -
i have send data udp. buffer field of type char. have encrypt/decrypt send. here's code encrypting/decrypting.
const unsigned buffer_size = 128; char data[buffer_size]; cout << "enter something." << endl; cin.getline(data, buffer_size); cout << data; char outchar[buffer_size]; for(int = 0; < strlen(data); i++) { //outchar[i] = data[i]; outchar[i] = encr[(int)data[i]]; cout << outchar[i]; } cout << "correct far" << endl; char transchar[buffer_size]; for(int j = 0; j < strlen(outchar); j++) { transchar[j] = decr[(int)outchar[j]]; cout << transchar[j]; // prints out decrypted text , unused space! }
the problem here using fixed size of char means there lot of unused space in buffer. in code provided, gets printed screen.
is there anyway dynamically increase , decrease buffer (char array) size not unused space?
edit: don't think clear: every letter has encrypted , decrypted! words not encrypted, letters make them are! advice given translate words array. need characters.
edit2: deleted.
edit3: russians, got love them. big vlad moscow!
the way dynamically allocate memory of required size. instead of doing manually can use standard class std::string
pass stored data char array can use member functions c_str()
or data()
for example
std::string data; std::cout << "enter something." << std::endl; std::getline( std::cin, data ); std::cout << data; std::string outchar; outchar.reserve( data.size() ); for(int = 0; < data.size(); i++) { //outchar[i] = data[i]; outchar.push_back( encr[ (int)data[i] ] ); std::cout << outchar.back(); }
to use std::string
need include header <string>
Comments
Post a Comment