+ 4

How to print last n characters of a string?

17th Jun 2018, 4:58 PM
Crystal!!!😎
Crystal!!!😎 - avatar
6 Respuestas
+ 7
thanks ~ swim ~ 😅😅😅😅
30th Jun 2018, 5:31 PM
Crystal!!!😎
Crystal!!!😎 - avatar
+ 5
In C : const char * s = ...; unsigned size = strlen(s); unsigned n = ...; printf("%s\n", s + (size - n)); In c++ (optimized) : std::string s = ...; unsigned n = ...; std::cout << (s.c_str() + (s.size() - n)) << std::endl;
30th Jun 2018, 9:45 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
in which language? string s =...; cout << s.substr(s.length()-n,n); works in c++ https://code.sololearn.com/c3qrJtjYUG2Z/?ref=app
17th Jun 2018, 4:59 PM
Max
Max - avatar
+ 3
be more specific with your language.. but heres a global tip... reverse the string.. then strip n number of characters of it from the front...
17th Jun 2018, 5:00 PM
᠌᠌Code X
᠌᠌Code X - avatar