0

here is the question: "write a program , which has as content "time is a great teacher but unfortunately it kills all its pupils"
and outputs th reverse string to the disk file out.txt.

My problem is that im stuck with the part on how to display the sentence in reverse anyone to help me with the codes?

Codes:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
   char a[100];

   ofstream f("out.txt");


   f<<"time is a great teacher but unfortunately it kills all its pupils ";
   f.close();


   ifstream f1("out.txt");
char ch;
   while(f1.get(ch)){

        ch--;
        cout <<ch;

   }

   f1.close();
return 0;
}
3
Contributors
2
Replies
19
Views
2 Years
Discussion Span
Last Post by VertexF3
0

you can use array or queue to store the output characters and then you can display those characters.
or you can directly reverse the string

MyStr = inputbox("Enter the String:")
nCnt = Len(MyStr)

For i = 1 to nCnt

   RevStr = Mid(MyStr,i,1)&RevStr

Next

Msgbox RevStr
0

Trying and use string objects from the #include <string> rather than a char array, because you can revrse a string object in one line of code.

#include <string>
#include <iostream>

int main()
{
    /*This is just a quick example you will need to load the string from the file just like you did with the char array.*/
    std::string reverse = "Backwards";


    /*String is a constructor of a string which creates a new string, begin and rend reverses the iterator*/
    reverse = std::string(reverse.rbegin(), reverse.rend());

    std::cout << reverse << std::endl;

    return 0;
}

Edited by VertexF3: Comment in the wrong place

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.