0

Sorry for the lack of clarity, English is my second language and it can be difficult some times to specify what I need.

I have an assignment to write a c++ program where it: (1)Reads a text file and determines which words are even and which are odd (2)Then it takes even words and reverses the order of characters in each even word.

So for example I have a moderate size text. It picks out even words and reverses their character order.

So far I have written this code and I don't know if it is any good to continue with because I don't know how to reverse the order of characters. Thank you for the help.

#include <iostream>
#include <string>
#include <string.h>
#include <cstdlib>
#include <fstream>
using namespace std;

int main()
{
    string word;
    ifstream f("text.txt");
    if (f.is_open())
    {
        while (f >> word)
        {
            if (word.length() % 2 == 0)
                cout << word << endl;
        }
        f.close();
    }
        else
            cout << "file is not open" << '\n';
    }
Sidabras
  • 3
  • 2
  • 4
    It's OK so far. You can use `std::reverse` to reverse a string. Or you can just think about an *algorithm* to reverse a string. It's not too difficult and it's good practise to think about how you would solve problems like this. Being able to invent code to solve problems is what programming is all about. – john Apr 27 '20 at 15:48
  • Looks like the only thing you are missing is the reverse function. Please come back after you attempted to write code for it and have added your code to the question. – drescherjm Apr 27 '20 at 15:50
  • See https://stackoverflow.com/questions/4951796/how-to-reverse-an-stdstring – auburg Apr 27 '20 at 15:51
  • 1
    You don't need `f.close();` and also I recommend avoiding 1 letter names for variables unless they are loop indicies. – drescherjm Apr 27 '20 at 15:52

2 Answers2

0

You just need to add to std::reverse to your code. I have also reviewed your code fragment.

Your code will look something like this after that:

#include <iostream>
#include <string>
// #include <string.h> -- no need of this header
#include <cstdlib> // not required at all for this code segment, maybe required in your actual code
#include <fstream>
#include <algorithm> // this is additional header that you need

using namespace std; // this is not a good practice though

int main()
{
    string word;
    ifstream f("text.txt"); // it is not recommended to use single letter variable names
    if (f.is_open()) // just keeping f instead of f.is_open() will work the same way
    {
        while (f >> word)
        {
            if (word.length() % 2 == 0)
                cout << reverse(word.begin(), word.end()) << endl; // using std::reverse here
        }
        f.close(); // not necessary statement but considered a good practice to prevent memory leaks and access locks.
    }
    else
        cout << "file is not open" << '\n'; // prefer using std::cerr instead of std::cout to log errors
                                            // and better error message will be "Unable to open text.txt!".
    return 0; // return something other than 0 in case of error
}
brc-dd
  • 10,788
  • 3
  • 47
  • 67
  • 1
    Thank you for your tips I actually did it with the std::reverse like john commented and it's identical like yours. I'm very glad to be making progress. Much love. – Sidabras Apr 27 '20 at 16:28
0

If you are looking to be a bit adventurous, you can write your own simple string reverse function using a forward and reverse iterator and simply swapping characters from the both ends of the string meeting in the middle, e.g.

std::string& strrev (std::string& s)
{
    std::string::iterator i = s.begin();
    std::string::reverse_iterator j = s.rbegin();

    for (; &(*j) > &(*i); i++, j++) {
        unsigned char tmp = *i;
        *i = *j;
        *j = tmp;
    }

    return s;
}

Of course std::reverse() does that for you, but as @john pointed out in the comments, being able to invent code to solve problems is what programming is all about.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85