reverse a string in an array

I have to write a program that takes a user input and prints it out backwards. i need to use an array, and if someone inputs hello the output needs to say olleh. this is what i have so far but when ever I run I get a ton of random symbols and beeps.

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
#include <iostream>
//#include <cstdlib>
#include <cstring>

int main()
{
	using std::cout;
	using std::cin;


	const int size = 128;
	char input;
	
	cout << "Enter a string: ";
	cin >> input;
	const char userinput[size]{input};

	for (size_t i = size - 1; i >= 0; i--)
	{
		cout << userinput[i];
	}


	cout << "\n\n\n";
	system("pause");
	return EXIT_SUCCESS;
}
Last edited on
On line 15, you are reading one character into input, rather than a string.

Why precisely are you using C-style strings (line 16) rather than C++ style strings?

-Albatross
I don't know how you are reading in to the array, because I am unfamiliar with C. I don't even know what line 16 is doing.

when you do get those elements stored, just use a for loop to print in reverse.
1
2
for (i = userinput.size(); i > 0; i--)
cout << userinput[i] << endl;
Last edited on
strings can be broken into char arrays without actually needing to store them...but you could store each individual letter if you so chose, but if you don't need to, just get the string, and reverse it like disturbed said...

this works

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
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <string>

using namespace std;
int main()
{



	//const int size = 128;
	string input;
	
	cout << "Enter a string: ";
	cin >> input;
	//int count = 0;
	//for(int i = 0; i < input.length(); i++)
		//count++;
	//const int size = count;
	//char *input2 = new char[size];
	
	for (int i = input.length(); i > 0; i--)
	{
		cout << input[i-1];
	}


	cout << "\n\n\n";
	system("pause");
	//delete[] input;
	return EXIT_SUCCESS;
}
I went ahead and did it for the char array too...using getline will allow the user to input a sentence and you can reverse the entire sentence

p.s...I was using v.s., so I had to dynamically allocate memory for the size of my array since it depends on the size of the string the user inputs...you may not need to use "new" and "delete" in your case...if you are having compilation issues because of these. try removing them and implementing this approach without the dynamic allocation

have fun

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 <cstdlib>
#include <cstring>
#include <vector>
#include <string>

using namespace std;
int main()
{



	//const int size = 128;
	string input;
	
	cout << "Enter a string: ";
	getline(cin, input);
	char *sa = new char[input.length()];

	for (int i = input.length(); i > 0; i--)
	{
		sa[i] = input[i-1];//this is getting the char's and storing them in an array
	}

	for (int i = input.length(); i > 0; i--)
	{
		cout << input[i-1];//all we are doing here is using the string to output the 

	}
	cout<< endl;
		for (int i = input.length(); i > 0; i--)
	{
		//cout << input[i-1];individual char's
		cout<<"["<< i << "]: " << sa[i] << endl;
	}


	cout << "\n\n\n";
	system("pause");
	delete[] sa;
	return EXIT_SUCCESS;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstring>

const int MAX = 100;

int main()
{
	char input[MAX] = { NULL };

	std::cout << "Enter a string: ";
	std::cin.getline(input, MAX);

	for (int i = strlen(input)-1; i >= 0; i--)
	{
		std::cout << input[i];
	}
	std::cout << std::endl;
	return 0;
}
Last edited on
by putting const int MAX = 100; outside of the main, does that mean it's a global variable and can be called from anywhere in the project?
Topic archived. No new replies allowed.