Reverse a string with no loops #include<fstream>

Apr 12, 2012 at 6:19am
Well, I've been working on this for the past 2 hours, with little to gain... I am supposed to take a string and reverse it with no loops. in this assignment, we are using fstream functions. This is what I've 'evolved to' this does not work whatsoever, but I am showing I've tried to do some work. I very well may have the wrong concept with some of these fstream functions, and I welcome any extra knowledge on the subject that seems unclear. while I've done quite a bit of internet research(I have googled it - lots of parts of it too) I would like pointers, and perhaps sample code(that doesn't actually solve my problem but shows me how these operators work) Thanks in advance for any help. Going to get some rest so I can finish my program in the morning.

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

int main()
{
	ifstream input;
	string forward;

	cout << "Please enter a string to see its reverse. (Will display reverse twice)" << endl;
	input.open("information.txt");
	cin >> forward;
	//reverseString(forward, findStringSize(forward)); a function I'm not actually calling
	input << forward;
	complexReverseString();
	
	input.close();


}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int complexReverseString()
{
	string reverse;
	ofstream output("information.txt", ios::ate);//open the file for output backwards

	int sizeOfString = sizeof("information.txt"); //find the size of string, thought I might need it



	advance("information.txt", 1); //while I know this doesn't work, it's kind of the track I'm thinking about...
	cout >> reverse;//I know this doesn't do anything, however I will have to cout at some point

        return reverse;

}
Last edited on Apr 12, 2012 at 6:20am
Apr 12, 2012 at 6:34am
I would think the only way to do this without an explicit or implicit (via a call to the standard library) loop would be recursion. I don't see that fstreams have anything to do with the problem.

Apr 12, 2012 at 9:44am
The simplest way to do that without loop is to use std::string::reverse_iterator.

Here is an example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main()
{
   std::string source( "Hello eyesofhope" );

   std::cout << "The original string is \"" << source << "\"\n";

   std::string target( source.rbegin(), source.rend() );

   std::cout << "The reversed string is \"" << target << "\"\n";

   return ( 0 );
}
Apr 12, 2012 at 4:31pm
This would better be reserved for a "beginners" question, but why do some programmers use the std:: commands in front of most things? I understand it's calling the standard library, but isn't it easier to call using namespace std;? So far(and I really am kind of a beginner, so I was seriously asking) my class has never used the "std::" command. If I were to use the namespace std and still use this as an example, would it look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using mainspace std;

int main()
{
   string source( "Hello eyesofhope" );

  cout << "The original string is " << source << endl;

   string target( source.rbegin(), source.rend() );

   cout << "The reversed string is " << target << endl;

   return 0;
}


that just looks like more of the formatting I use. I don't know if it's correct or not.
Apr 12, 2012 at 5:05pm
To answer my own question, it's this(with my other main int at top):

1
2
3
4
5
6
7
8
int complexReverseString(string userInput)
{
	string source(userInput);
	string target( source.rbegin(), source.rend() );
	cout << "The reversed string is " << target << endl;

	return 0;
}


Thanks for the help! :)
Last edited on Apr 12, 2012 at 5:07pm
Topic archived. No new replies allowed.