please help me i really need this badly

i'm trying to write all the possible code but the outcome i needed is not right

i write a program that you input a string then the output will be the last letter of you input will come first then follows

example: input 
                    car
               output
                    rca
 #include<iostream.h>
 #include<string.h>

  int main()
  { 
        int x,a;
        char word[20];
        cout<<"Enter a string: ";
        cin.getline(word,20)"
        a=strlen(word);
        cout<<"the new word ";
        for(x=a-1;x<=a;x++)
            for(j=2;j>x;j--)
             cout<<word[x]

OUTPUT

when i input car
the out is only letter r

please help me ....i need it now

thank you so much!! !

Edited 3 Years Ago by happygeek: fixed formatting

you are making it too difficult! All you have to do is find the last character, save it to another variable (or just print it), replace it with a 0 to truncate the string, then print the rest of the string as you normally would. There are no loops needed.

>how i will find the last character??
strlen gives you the length of the string. If you use that number as an index into the string, it always gives you the null character. Provided that number isn't 0, you can subtract 1 and it'll give you the last character in the string. So:

const char last_character ( const char *s )
{
  size_t len = strlen ( s );

  if ( len > 0 )
    --len;

  return s[len];
}

This function gives you the last character, or '\0' if the string is empty.

JUST REPLACE a WITH 0 IN --for(int x=a-1;x>-=0;x--) THAT YOU COULD ACCESS THE WHOLE LENGTH OF THE STRING

YOU KNOW THAT CHAR ARRAYS ARE ZERO INDEXED THEY START FROM 0 TO (LENGTH -1)

This article has been dead for over six months. Start a new discussion instead.