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...
I am trying to tokenize a string using a function by passing the char string[] and...
I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function. I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]). This is my code below: int main() {    // Declare variables    char str[] = "this is a test only...
this is my code in python I am trying to open a file and then print...
this is my code in python I am trying to open a file and then print the contents on my console but when i run it nothing prints in the console def file_to_dictionary(rosterFile): myDictionary={}    with open(rosterFile,'r') as f: for line in f: myDictionary.append(line.strip()) print(myDictionary)             return myDictionary    file_to_dictionary((f"../data/Roster.txt"))      
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 return a string from certain position of the sentence on C++, like...
I am trying to return a string from certain position of the sentence on C++, like a function/ statement that is equivalent to excel mid function.
Java Question I have a Queue containing String type taking in a text file with an...
Java Question I have a Queue containing String type taking in a text file with an iterator. I need to use ArrayList and HashMap for freqHowMany to get a frequency table in descending order by the highest frequency words in the text. Any help would be much appreciated and thanks! final Iterator input = new Scanner(System.in).useDelimiter("(?U)[^\\p{Alpha}0-9']+"); final Queue queueFinal = new CircularFifoQueue<>(wordsLast); while (input.hasNext()) { final String queueWord = input.next(); if (queueWord.length() > minimumLength) { queueFinal.add(queueWord); // the oldest item...
Hi, (C programming) I am looking to write a program that will encrypt a text file...
Hi, (C programming) I am looking to write a program that will encrypt a text file automatically once program has opened, and have the option to decrypt it in a menu in my program. I have multiple text files that I need to encrypt, and would like if the program could encrypt all of them at once. I would also only like to decrypt the a text file once the name has been entered into a scanf function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT