+ 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.
11 Réponses
+ 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.
+ 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...
+ 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;
}
+ 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.
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
0
Dude I gave you the answer yesterday :)
0
@demeth iam trying to overload it but i can't . if you have time can you make it please :)
0
@ripper ya but i can't use it , The program didnot run
can you make it as a complete code please ?
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:)
0
it had run correctly but i havenot use The (<<) operator yet
0
thanks :)