c++11 - perplexing things in c++ -
i trying copy array of chars, other array of chars in reversed order.
this method:
void reversstring(char* str){ char* ptr = str; int = 0; // getting length of str/ptr array while (*(ptr + i) != '\0'){ = + 1; } char revstr [i]; char * revstrchar = &revstr[0]; int revstrpos = 0; cout << *(ptr + 3) << endl; }
here trying copy in normal order, if print last letter of input ("abcd"), nothings happens. prints empty line.
but if delete declaration of new char array:
void reversstring(char* str){ char* ptr = str; int = 0; // getting length of str/ptr array while (*(ptr + i) != '\0'){ = + 1; } //char revstr [i]; //char * revstrchar = &revstr[0]; //int revstrpos = 0; cout << *(ptr + 3) << endl; }
then prints last letter correctly, "d". not understand how declaring new char array influences output! (compiler mingw, os win10)
you tagged c++11 . . . why not more modern stl way:
#include <iostream> #include <string> #include <algorithm> int main() { std::string str{"12345abc"}; std::string copy = str; std::reverse(copy.begin(), copy.end()); std::cout << copy << std::endl; return 0; }
output:
cba54321
Comments
Post a Comment