reversing the order of letters of words


Plz help me in a concept of reversing order of letters of input word without using string or pointer.Just a simple turbo c++ program I want!

Assuming you want to reverse the contents of an array filled by whatever was entered from the keyboard, here's one I wrote for another member. http://www.cplusplus.com/forum/beginner/132247/#msg711746
But i want a simple program without any array
closed account (j3Rz8vqX)
How would you store those letters? String of characters? pointer to string of characters? Array of characters? X characters that represent a word?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cctype>

std::istream& reverse_word(std::istream& in, std::ostream& out)
{
    char ch;
    
    if (in.get(ch) && !std::isspace(ch))
    {
        reverse_word(in, out);
        out << ch;
    }

    return in;
}

int main()
{
    for (unsigned i = 0; i < 5; ++i)
    {
        reverse_word(std::cin, std::cout);
        std::cout << '\n';
    }
}


http://ideone.com/Y4PvRK
i just want a program that inputs a word for example maher and reverses it like reham char type
closed account (j3Rz8vqX)
@maher123: Provide what you have so far; we're uncertain of the resources available to you.

@cire, Nice!
An input controlled loop:
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
#include <iostream>
#include <cctype>

std::istream& reverse_word(std::istream& in, std::ostream& out, bool &run)
{
    char ch;

    if (in.get(ch) && !std::isspace(ch))
    {
        run=true;
        reverse_word(in, out, run);
        out << ch;
    }

    return in;
}
int main()
{
    bool run;
    do{
        run = false;
        reverse_word(std::cin, std::cout, run);
        std::cout << '\n';
    }while(run);
}

Will break when null is entered.
closed account (jvqpDjzh)
Suppose you have a char array (char name[6] = "Obama";) and this string has a length, which is 6 (remember the null terminated character = '\0'). The first character of the string is actually numbered 0, the second 1, and so on til th 5th character. So basically you have:

O = [0]
b = [1]
a = [2]
m = [3]
a = [4]
'\0' = [5]

What you have to do is put the character 'a' in the position [0].
I suggest you to create another empty char array of the same size, for example: char reversed_name[6];

In this new array (reversed_name) you have to put in the first position, which actually is the position [0], the last character of the other string (or array), which is actually at the position [4] (we don't count the '\0', because we don't want to reverse it of course, lololoolool), so you have this:

reversed_name[0] = name [4];
reversed_name[1] = name [3];
reversed_name[2] = name [3];
reversed_name[3] = name [2];
reversed_name[4] = name [1];

Now you have the name reversed, which is saved in reversed_name. You can do something like that:
1
2
3
4
5
6
7
8
9
    char name[6] = "Obama";
    char reversed_name[6];

    for(int i=0, j=4; i<5 && j>=0; i++, j--)
        reversed_name[i] = name[j];
    reversed_name[5] = '\n';

    std::cout << "The name is: "<<name<<"\n\n";
    std::cout <<"The reversed name is: "<<reversed_name<<"\n\n";

i just want a program that inputs a word for example maher and reverses it like reham char type


How would like that reversed char type ("reham") stored ?

if you just want to output it right away @cire's answer is the answer
closed account (jvqpDjzh)
@rmxhaha (208)
if you just want to output it right away @cire's answer is the answer


Plz help me in a concept of reversing order of letters of input word without using string or pointer.Just a simple turbo c++ program I want!
Read this and tell me what I have done!
Last edited on
@zwilu, Sorry your post hasn't exist yet when I write those.
( I didn't refresh before replying )

without using string or pointer

I thought by string he is refering to std::string and by pointer he is refering to array ( both dynamic and static ). It confuse me a little.

What you did is explain how do normal people reverse an array...
Let's just wait to for OP to explain better what he wants or tells us he gets what he wants
closed account (jvqpDjzh)
What you did is explain how do normal people reverse an array...
Let's just wait to for OP to explain better what he wants or tells us he gets what he wants
Yes, if it doesn't say nothing else, we can't help more!

zwilu, Sorry your post hasn't exist yet when I write those.
Np dude ;)

Dear experts,
I want you to help me at the array reversing any word that is input for example when i input PAKISTAN it prints out NATSIKAP . PLz Keep in mind that it should be of beginner level program, not to expert.
If just want to input then output the reverse of it then do just that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>

int main(){
   string input; // declare variable

   cin >> input; // get input from user
   
   // output it in reverse order
   for( int i = input.length(); i >= 0; -- i ){
      // output the char one by one according to character's index ( i-th letter in the word )
      cout << input[i];
   }
   return 0;
}


string makes coding easier so try using it
Last edited on
That, however, poses a question. What should a beginner learn?
The features of the C++11 as whole and Standard Library in particular, which are always useful, or the only archaic tidbits that some abandonware supports?

The std::string is essentially just a decorated array, although much simpler to use.

When you ruled out string, array, and pointers, you were left with (ingenious) recursive solutions, which look far from simple. Sure, you could learn a lot from them, but it does not sound like they are the "beginner level" that you are looking for.

Would this be too simple?
1
2
3
4
5
std::string word;
if ( std::cin >> word ) {
  std::reverse( word.begin(), word.end() );
  std::cout << word << '\n';
}


There are free compilers that support (most) of C++11, so tools should not limit learning C++.
closed account (jvqpDjzh)
1
2
3
4
5
std::string word;
if ( std::cin >> word ) {
  std::reverse( word.begin(), word.end() );
  std::cout << word << '\n';
}
This doesn't help in the concept of reversing a string!
Why not? The string is now reversed, and quite possibly in a more efficient manner than what you could do, as well as in a far simpler and readable fashion. What you might have meant is that it doesn't help in pointer manipulations, though considering arrays and pointers are out that isn't what the OP is looking for either.
closed account (jvqpDjzh)
Ok, you are right.
closed account (j3Rz8vqX)
concept of reversing order of letters
Definition of concept:
an idea of what something is or how it works
http://www.merriam-webster.com/dictionary/concept

OP's specifics:
without using string or pointer
and
simple program without any array
and
I want you to help me [understand] the [process of] array reversing any word that is input

Assumption: character array and reversing array (not print manipulation)
A possible solution:
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
#include <iostream>
#include <cstring>  //For string length: strlen();
using namespace std;

int main()
{
    //Declare an array of characters: (c string)
    char cstring[20];

    //Use cins getline function to retrieve a string from user:
    cout<<"Enter the string\n\n: ";
    cin.getline(cstring,20);

    //Declare a variable of integer to store the size of array.
    int strSize = strlen(cstring);

    //Procedure: Reversing c string:
    for(int i=0;i<strSize*0.5;++i)
    {
        char ch = cstring[i];
        cstring[i] = cstring[(strSize-1)-i];
        cstring[(strSize-1)-i] = ch;
    }
    
    //Print c string:
    cout<<": "<<cstring;
    
    //Console exit prompt: in the case of command line execution:
    cout<<"\n\nPress enter to continue: ";
    cin.get();

    return 0;
}
Enter the string

: PAKISTAN
: NATSIKAP

Press enter to continue:
Last edited on
thanks so much
Topic archived. No new replies allowed.