Brothersoft.comWindows|Mac|Mobile|Games

|Message

Solve software problem quickly Share experience to help others Improve the ability of solving problem
How to Take a String From an Input File and Reverse It in C++

11/08/2011 00:14 by ThomasOakes

First answer posted by KathyBren at 11/08/2011 00:14
Add Your Answer
10~1000 characters in length CAPTCHA:
1 Answers
  • KathyBren
    1. Declare a sting to reverse. Enter the following into the text editor:

      #include

      #include

      #include

      using namespace std;

      int main(){

      string line1, line2;

      ifstream file ("filename.txt");

      if (file.good())

      {

      getline(file, line1);

      }

      file.close();

      }

      The goal, then, is to take "line1" (which contains a line from the file "filename.txt") and reverse it.

    2. Create a loop to iterate over the string, in reverse. Enter the following into the text editor:

      int i = (int)line1.length() -1;

      for (i; i >= 0; i--)

      {

      line2 += line1[i]

      }

      The "for" loop starts with "i" referring to the position of the last character in the sentence. It appends that letter to line2, and moves to the next character of line1 in reverse.

    3. Print out the reverse line. Enter the following into the text editor, following the loop:

      cout

  • Was this answer helpful? 00 · 11/08/2011 00:14
Add Your Answer
10~1000 characters in length CAPTCHA:
Related Questions
Q:What Is the File Extension L?

A:Because file extensions are chosen based on the preferences of programmers during software development, it is possible for unrelated files to ...(more)

Q:What Is the File Extension Cwp?

A:Music production software is used to edit songs and then save the resulting music as a digital file. There are a number of music production so...(more)

Q:What Is the File Extension CID?

A:Because file extensions are selected by programmers based upon their own preferences, it is possible for unrelated types of files to share a c...(more)

Q:How to Convert TEX to TGA

A: Download, install and run the ZaZ Tools software suite. Double-click on the SlimTex application. Click on the "Batch to TGA" tab at the ...(more)

Q:How to Convert a DVF File Into a DVD

A:Convert the DVF File to WAV Format Right-click on the DVF file to show the Windows context menu. Click the "Open With" option. A list of a...(more)

Added Successfully!

×

Are you sure to delete your answer?

NoYes

×

Are you sure to choose it as the best answer?

NoYes

×

Voted Successfully!

×

You can't vote for yourself

×

You can't choose your own answer

×