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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -