How to print last n characters of a string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to print last n characters of a string?

17th Jun 2018, 4:58 PM
Crystal!!!๐Ÿ˜Ž
Crystal!!!๐Ÿ˜Ž - avatar
4 Answers
+ 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
แ Œแ ŒBrains[Abidemi]
แ Œแ ŒBrains[Abidemi] - avatar