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;
}

Recommended Answers

All 2 Replies

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

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.