reverse symbols in a file

Q: given a txt file, create a new one out of it by reversing the symbols from it and flipping the strings upside down.
Ex:
(input file)
q w e r
a s d f
z x c v

==
(output)
v c x z
f d s a
r e w q

here's what i got but it doesn't flip the lines but simply outputs it in reverse as one line. and it doesn't put it in a new file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    std::ifstream file("numbers.txt");
    std::string content;
    std::vector<std::string> numbers;

    while (file >> content)
        numbers.push_back(content);
    for (int i = numbers.size() - 1; i >= 0; i--)
        std::cout << numbers[i] << ' ';
    std::cout << std::endl;

    return 0;
}
Last edited on
Hello laura fidarova,

I was thinking of starting with something like this:
1
2
3
4
5
    std::ifstream file("numbers.txt");
    // <--- Should check if file is open and usable.
    std::string content, flippedContent;
    std::vector<std::string> numbers;
    std::vector<std::string> flippedNumbers;


After reading the file into the first vector flip the string and store it in the new vector then you can output the new vector in reverse order.

I have not change any code yet. It will take a few minutes.

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void backward( istream &in )
{
   string line;
   if ( getline( in, line ) )
   {
      backward( in );
      cout << string( line.rbegin(), line.rend() ) << '\n';
   }
}

int main()
{
   ifstream in( "numbers.txt" );
   backward( in );
}
Hello laura fidarova,

Not as compact as what lastchance did, but something to think about and compare:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
    std::istringstream file
    {
        "q w e r\n"
        "a s d f\n"
        "z x c v\n"
    };

    //std::ifstream file("numbers.txt");
    // <--- Should check if file is open and usable.
    std::string content, flippedContent;
    std::vector<std::string> lines;
    std::vector<std::string> flippedNumbers;

    while (std::getline(file, content))
        lines.push_back(content);

    for (size_t idx = 0; idx < lines.size(); idx++)
    {
        content = lines[idx];

        for (auto rit = content.rbegin(); rit < content.rend(); rit++)
        {
            flippedContent += *rit;
        }

        flippedNumbers.emplace_back(flippedContent);

        flippedContent.clear();
    }

    for (auto rit = flippedNumbers.rbegin(); rit < flippedNumbers.rend(); rit++)
        std::cout << *rit << '\n';

    std::cout << std::endl;

    return 0;  // <--- Not required, but makes a good break point.
}

The string stream is just a quick replacement for the file and can be removed to use the file that you first started with.

Andy
And another ...

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
   ifstream in( "numbers.txt" );
   string line, document = "\n";
   while( getline( in, line ) ) document += '\n' + line;
   cout << string( document.rbegin(), document.rend() );
}
> reversing the symbols from it and flipping the strings upside down

We can't 'flip the strings upside down' in one pass other than by reading in the entire contents first. Reading the contents into a string and writing that string in reverse would do the job. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <fstream>

// read the entire contents into a string
std::string get_text( std::ifstream file )
{
    std::string text ;
    char c ;
    while( file.get(c) ) text += c ;
    return text ;
}

// write reverse "upside down"
bool write_flipped( std::ofstream file, const std::string& text )
{
    // avoid creating a second potentially very long temporary string
    for( auto iter = text.rbegin() ; iter != text.rend() ; ++iter ) file.put( *iter ) ;
    return bool(file) ;
}

int main()
{
    const std::string input_file_name = "in.txt" ;
    const std::string output_file_name = "out.txt" ;

    // create a test file
    std::ofstream(input_file_name) << "1. first line - A\n2. the second line - B\n3. and a third line - C\n" ;

    // read the input file into a string and write it in reverse
    const std::string text = get_text( std::ifstream(input_file_name) ) ;
    write_flipped( std::ofstream(output_file_name), text ) ;

    // dump the contents of the two files to verify the result
    std::cout << "input file " << input_file_name << "\n-----------\n"
              << std::ifstream(input_file_name).rdbuf() ;
    std::cout << "\noutput file " << output_file_name << "\n-----------\n"
              << std::ifstream(output_file_name).rdbuf() ;
}

http://coliru.stacked-crooked.com/a/7ae2d6c71bf74ade
thank you all so much!! much appreciated:))
For another way, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <fstream>
#include <iostream>
#include <string>
#include <iterator>
#include <iomanip>
#include <algorithm>

int main()
{
	std::ifstream ifile("numbers.txt");
	std::ofstream ofile("reverse.txt");

	if (!ifile || !ofile)
		return (std::cout << "Cannot open files\n"), 1;

	std::string content((std::istream_iterator<char>(ifile >> std::noskipws)), std::istream_iterator<char>());

	std::copy(content.rbegin(), content.rend(), std::ostream_iterator<char>(ofile, ""));
}

Reversing lines and individual elements in a line using std::vector and std::string and their associated iterators:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

int main()
{
   std::ifstream fin("numbers.txt");
   std::ofstream fout("reverse.txt");

   if (!fin || !fout)
   {
      std::cerr << "Unable to open files!\n";
      return 1;
   }

   std::vector<std::string> contents;
   std::string fline;

   while (std::getline(fin, fline))
   {
      contents.push_back(fline);
   }

   // print forward (for confirmation)
   for (auto line { contents.cbegin() }; line != contents.cend(); ++line)
   {
      for (auto chr { line->cbegin() }; chr != line->cend(); ++chr)
      {
         std::cout << *chr;
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   // print reverse (for confirmation)
   for (auto line { contents.crbegin() }; line != contents.crend(); ++line)
   {
      for (auto chr { line->crbegin() }; chr != line->crend(); ++chr)
      {
         std::cout << *chr;
      }
      std::cout << '\n';
   }

   // print reverse into a file
   for (auto vline { contents.crbegin() }; vline != contents.crend(); ++vline)
   {
      for (auto chr { vline->crbegin() }; chr != vline->crend(); ++chr)
      {
         fout << *chr;
      }
      fout << '\n';
   }
}
Topic archived. No new replies allowed.