Hello. I'm learning programming, and C++.
I've got a question, but I couldn't solve my problem so far.
I need to use arrays and only basic stuff to solve this:
Create a program that reads a word from the user and then print (cout) this word on contrary. It's simple, I know, but I can't do it, so any help will be appreciate. I've tried some stuff but I don't get how I will get the proper values to do this. All I know is that I can use variable.lenght().
Create a program that reads a word from the user and then print this word.
There's really nothing difficult in it, simply include the 'string' header file and inside the main() function, create a string object string name; then let the user input assign a name to it cin>>name -OR- you could make an array of chars instead of a string object, like that: char name[10];cin>>name; then simply send name to cout. If you want to use the .length() or .size() member function then use a string object instead of an array or if you want to view the size of your character array then try using the sizeof() member function, hope this helps.
I'm sorry guys, yes, I meant string in reverse, for example:
douglas = salguod
We didn't learned anything about objetcts, just starting on arrays.
Edit: Wow, I can't believe it was so simple, it was really the first thing I had in mind, but didn't tried it out because I thought it wouldn't work. I still don't get it right, this only works withs strings? Can i use the getline instead of the cin on this case as well?
#include <iostream>
using namespace std;
int main()
{
cout << "Insert a Word: ";
string word;
cin >> word;
for (int cont=word.length() -1; cont>=0; --cont){
cout << word[cont];
}
}
That's it. But I'm curious now, how would this work using the char?
#include <iostream>
#include <conio.h>
usingnamespace std;
int main(){
int length;
char ARRAY[]={};
cout<<"Enter Length of the word: ";
cin>>length;
cout<<"Enter Word: ";
for (int i=0; i<length; i++){
cin>>ARRAY[i];
}
cout<<"Reverse Order of the above word is: ";
for (int k=length; k>=0; k--){
cout<<ARRAY[k];
}
getch();
return 0;
}