Program to do shift left operator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Program to do shift left operator

i want to make aProgram to do shift left operator(<<) On a string like ("Ahmed") by 3 to get The letters ("ed") only after shifting pls.

5th Dec 2016, 9:40 PM
Ahmed Mansy
Ahmed Mansy - avatar
11 Answers
+ 2
Here you go, bro) http://www.sololearn.com/app/sololearn/playground/c4EmCiQytyQD/ Have done both shift functions, with over-shift protection and negative values support. That was a really interesting experience implementing this. Sometimes adding custom behavior like this can make your code look more elegant and effective. And mysterious, but it totally worth it.
5th Dec 2016, 11:45 PM
Demeth
Demeth - avatar
+ 1
If you really want to use the << operatpr then you need to overload it, to be abel to do what you want.. but it is a lot of work for nothing...
5th Dec 2016, 11:11 PM
Ripper
Ripper - avatar
+ 1
@Ahmed Here you a shift function that i made for you:) #include <iostream> #include <string> using namespace std; string shift (string value, int pos){ return value.substr(pos, value.length() - pos); } int main() { cout<< shift("ahmed",3); return 0; }
5th Dec 2016, 11:18 PM
Ripper
Ripper - avatar
+ 1
I don't understand why can't we shift in nibbles ?. << (n * 4). Here n is number of characters to be shifted. Please note same logic will not work for right shift.
6th Dec 2016, 2:13 AM
Balaji Kannadassan
Balaji Kannadassan - avatar
0
you can't do bit-shifting on strings, as it shifts binary data, filling places with zeros. however, you can try to overload shift operator and make a custom implementation for strings only
5th Dec 2016, 10:04 PM
Demeth
Demeth - avatar
0
Dude I gave you the answer yesterday :)
5th Dec 2016, 10:16 PM
Ripper
Ripper - avatar
0
@demeth iam trying to overload it but i can't . if you have time can you make it please :)
5th Dec 2016, 10:19 PM
Ahmed Mansy
Ahmed Mansy - avatar
0
@ripper ya but i can't use it , The program didnot run can you make it as a complete code please ?
5th Dec 2016, 10:20 PM
Ahmed Mansy
Ahmed Mansy - avatar
0
#include <iostream> #include <string> using namespace std; int main() { string name ="ahmed"; cout<<name.substr(3, name.length() - 3); return 0; } I just tried it... it works just fine:)
5th Dec 2016, 10:25 PM
Ripper
Ripper - avatar
0
it had run correctly but i havenot use The (<<) operator yet
5th Dec 2016, 10:32 PM
Ahmed Mansy
Ahmed Mansy - avatar
0
thanks :)
5th Dec 2016, 11:50 PM
Ahmed Mansy
Ahmed Mansy - avatar