In: Computer Science
Left shift.
I am trying to left shift a string located in a text file. I am using c++ and the goal is to left shift by 1 character a string of length N, and output on stdout all of the performed shifts. Can someone tell me what I am doing wrong, and tell me what I can fix?
i.e: text file contains: Hi there!.
Output on stdout:
Hi there!, i there!H, _there!Hi, there!Hi_,...., !Hi_there. #here "_" represent space.
-------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
void shiftleft (vector <string> array, int d)
{
reverse(array.begin(), array.begin() + d);
reverse(array.begin() + d, array.end());
reverse(array.begin(), array.end());
}
int main()
{
string input_line;
vector<string> value;
ifstream file("LeftShift2.txt");
if(file.is_open())
{
while(getline(file, input_line))
{
value.push_back(input_line);
for(int i = 0; i < value.size(); i++)
{
cout << value[i] << "---->"<<endl;
shiftleft (value, 1);
}
}
}
}
--------------------------------------------------------------------------------
LeftShift2.txt
Hi there!
I have got the output where i have used strings instead of vectors since in strings you can operate on individual characters
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
void shiftleft (string& array, int d)
{
reverse(array.begin(), array.begin() + d);
reverse(array.begin() + d, array.end());
reverse(array.begin(), array.end());
}
int main()
{
string input_line;
ifstream file("LeftShift2.txt");
if(file.is_open())
{
while(getline(file, input_line))
{
for(int i = 0; i <input_line.size(); i++)
{
cout << input_line<< "---->"<<endl;
shiftleft (input_line, 1);
}
}
}
}
Output