Need Help with Array - Strings

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().
Last edited on
What is meant by "this word on contrary"?
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.
Last edited on
If I understand the question right, you supposed to output the string in reverse.
This would be my suggestion for the output loop:

1
2
for(int i = variable.lenght() - 1; i >= 0; --i) std::cout << variable[i];
std::cout << std::endl;

I assume "variable" is defined as string.

All the other stuff (includes, main(), cin, ...) is pretty basic straight forward coding you've probably done a couple of times already.
He didn't say anything about outputting the string in reverse form, this is confusing :s
@Uk Marine
That's right. I assumed so because of this: "this word on contrary". Sounds like a unusual translation. Maybe I'm wrong.
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?
Last edited on
//with char Array this will be the code,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>
using namespace 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;
        }
Last edited on
Topic archived. No new replies allowed.