I am a beginner & I need to implement this program which states that I have to reverse whatever is given in input via pointers...

For Example:
i) Input: This is reverse order program
Output: program order reverse is This

ii) Input: This is exercise eight
Output: eight exercise is This

I made it a little bit, but it outputs to "is input" if i input "The input is" string.

using namespace std;

int main()
    char sentence[80]={0};
    cout<<"Input the string: ";

    int length=strlen(sentence);
    int check=0;
    for(int i=length; i>0; i--)
        if(sentence[i]!=' ')
            for(int j=i; j<(check+i); j++)
            cout<<" ";
    return 0;


6 Years
Discussion Span
Last Post by kes166

You are using the space as a deliminator, meaning you are breaking the words apart with a space. In the sentence "The input is" you get the space before is, then retrieve the word "is", you then get the space before input, and retrieve "is input" but there is no space before the word "The".

The letter 'T' of the word "The" resides in sentence[0]. Currently as written, line 12 will not execute when i = 0. You'll need to change that to

for(int i=length-1; i>=0; i--)

Your length value will be 12, so you will only need to reference sentence[11] through sentence[0]. Once you include the location sentence[0] in your for loop, you will need to check for that special condition, namly when i = 0. You can do that in your if statement in line 14. If i equals 0, then that is your first word of the sentence.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.