Array slicing in c++ -
i trying slice last 4 characters off character array , tried method python uses without success;
char *charone = (char*)("i need last four") char *chartwo = charone[-4:] cout << chartwo << endl;
i want code return;
four
but c/c++ doesn't seem easy...
where find simple alternative return last 4 chars of 1 char array char array?
try:
int len = strlen(charone); char *chartwo = charone + (len < 4 ? 0 : len - 4);
in c++, can replace with:
char* chartwo = charone + (std::max)(strlen(charone), 4) - 4;
the code uses special property of c strings works chopping off beginning of string.
Comments
Post a Comment