using C++ language, Create (fill-in) the following functions: string rotateStrin
ID: 3800635 • Letter: U
Question
using C++ language,
Create (fill-in) the following functions:
string rotateString(string to_rotate, int direction, int offset) - Rotate a string in the direction indicated by the number of characters indicated by the offset. If the offset is zero or string is empty return or if the direction is invalid return the original string.
Example 1: string to rotate: xyz123
Direction: Right
Offset: 3
Newstring: 123xyz
Example 2: string to rotate: fizzybeer
Direction: Left
Offset: 5
Newstring: beerfizzy
Explanation / Answer
Function code :
NOTE : please include the following header file "#include <algorithm>
//direction = 1 for LEFT and direction = 0 for Right
Example program :-
#include <iostream>
#include <algorithm> // std::rotate
using namespace std;
string rotateString(string to_rotate,int direction,int offset){
if(direction == 1){
rotate(to_rotate.begin(), to_rotate.begin() + offset, to_rotate.end());
return to_rotate;
}
if(direction == 0){
rotate(to_rotate.begin(), to_rotate.end() - offset, to_rotate.end());
return to_rotate;
}
}
int main() {
string s="feezybeer";
cout<< rotateString(s,1,5)<<endl;
s="xyz123";
cout<<rotateString(s,0,3);
return 0;
}
Output:
beerfeezy
123xyz
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.