Method of the string class

Hi, I am trying to write a program that will ask the user for their name and a random input of characters. Then, it should output to the screen the random string of characters, backwards, with their name in the middle of it. I am having trouble reversing the string, i think, and I am only having the name output at the moment. Also, would it be easier to use the 'insert' string method here? Here is my code at the moment. Still working on it.

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
 
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<fstream>

using namespace std;

int main()
{
	string name;
	string random;
	

	cout<<"Please input your name and a random string of characters, hit enter after your name"<<endl;
	getline(cin, name);
	getline(cin, random);

	string str1(random, int(random.length()), int(random.length())/2);
	string str2(random, int(random.length())/2, 0);
	cout<<str1<<name<<str2;

	system("pause");
	return 0;
}
Ok, I think this is better. I am still stuck on how to reverse the order of the random input however.

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
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<fstream>

using namespace std;

int main()
{
	string name;
	string random;
	

	cout<<"Please input your name and a random string of characters, hit enter after your name"<<endl;
	getline(cin, name);
	getline(cin, random);

	random.insert(int(random.length())/2, name);
	
	cout<<random;

	system("pause");
	return 0;
}
We haven't learned about that class yet, i think we are supposed to create our own way to do it. Why didn't my first one, or this one, work?

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
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<fstream>

using namespace std;


	string random;
	string revrandom;
int main()
{
	void reverse();
	string name;


	

	cout<<"Please input your name and a random string of characters, hit enter after your name"<<endl;
	getline(cin, name);
	getline(cin, random);

	reverse();

	random.insert(int(random.length())/2, name);
	
	cout<<random;

	system("pause");
	return 0;
}
void reverse()
{
	int i;
	int j = (random.length()-1);
	for(i = j; i>=0; i--)
	{
		revrandom[i-j] = random[j];
	}

	return;
}
I changed it a little bit, my output is still not correct though. If i put in say 'mike 1234', i get 42mike34 as the output.

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
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<fstream>

using namespace std;


	string random;
	
	void reverse();
int main()
{
	
	string name;
	cout<<"Please input your name and a random string of characters"<<endl;
	cout<<"hit enter after your name"<<endl;
	getline(cin, name);
	getline(cin, random);

	reverse();

	random.insert(int(random.length())/2, name);
	
	cout<<random;

	system("pause");
	return 0;
}
void reverse()
{
	int i;
	int j = (random.length()-1);
	for(i = j; i>=0; i--)
	{
		random[i-j] = random[j];
	}

	return;
}
How can your reverse function work properly without having anything passed to it to reverse?

void reverse(random&) <--this is more similar to what you are looking for.
Last edited on
I was trying to use global variables
Try to avoid global variables.

Q: Can you tell the essential difference of these:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <class BidirectionalIterator>
  void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
  while ((first!=last)&&(first!=--last)) {
    std::iter_swap (first,last);
    ++first;
  }
}

void reverse()
{
  int j = random.length() - 1;
  for ( int i = j; i>=0; i-- ) {
      random[i-j] = random[j];
  }
}

A: One swaps, the other assigns.

You could do it differently too: copy to another string in reverse direction. Actually, your version is very close to that already.
Topic archived. No new replies allowed.