Question

In: Computer Science

Left shift. I am trying to left shift a string located in a text file. I...

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!



Solutions

Expert Solution

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


Related Solutions

Hi, I am trying to write a php file which control the text size, color and...
Hi, I am trying to write a php file which control the text size, color and the parameters of a registration page. My code is not working, the registration page always stay in the default color. Could I get some help ? My code is below. display file <?php session_start() ?> <!DOCTYPE html> <html> <head>    <meta charset="UTF-8">    <title>User display window</title>    <style>        .center { text-align:center}        </style>    </head> <body>    <fieldset>        <legend...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX INPUT: B C D A OUTPUT: D C B A #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]) {         int MAX = 100000;         int i =0;         int k =0;         int j =0;         char array[MAX];         char split[] = " ,.-!?()0123456789";         int n = 0;         char second[MAX];         printf("Please enter in a String: ");...
#Java I am reading from the txt file and I put everytihng inside of the String[],...
#Java I am reading from the txt file and I put everytihng inside of the String[], like that: String[] values = str.split(", "); But, now I have to retrieve the data from it one by one. How can I do it? Here is the txt file: OneTime, finish the project, 2020-10-15 Monthly, wash the car, 2020-11-10 Thanks!
I am trying to make a program in Python that opens a file and tells you...
I am trying to make a program in Python that opens a file and tells you how many lines, vowels, consonants, and digits there are in the file. I got the print out of lines, digits, and consonants, but the if statement I made with the vowels list isn't recognizing them. How can I make the vowels go through the if statement with the list? Am I using the wrong operator? Here is my program #Assignment Ch7-1 #This program takes...
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
Similar to in HW1, you'll implement a left shift of decimals in a string, with combining...
Similar to in HW1, you'll implement a left shift of decimals in a string, with combining of multiple like digits into a single digit of double the value. The main difference between HW1 and this is that now the string representing a row in 2048 can be of any length! You'll implement a Combine class in a comparable .java file. As before, the input string can only include the characters '_', '2', and '4'. Unlike before, it can be of...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
C# Simple Text File Merging Application - The idea is to merge two text files located...
C# Simple Text File Merging Application - The idea is to merge two text files located in the same folder to create a new txt file (also in the same folder). I have the idea, and I can merge the two txt files together, however I would like to have an empty line between the two files. How would I implement this in the code below? if (File.Exists(fileNameWithPath1)) { try { string[] file1 = File.ReadAllLines(fileNameWithPath1); string[] file2 = File.ReadAllLines(fileNameWithPath2); //dump...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT