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.
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...
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
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <string>
usingnamespace std;
int main()
{
//const int size = 128;
string input;
cout << "Enter a string: ";
getline(cin, input);
char *sa = newchar[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;
}