Search code examples
turbo-c++turbo-c

How can i rewrite this program so that it will print the value of text in reverse order


#include<iostream.h>
#include<conio.h>
#include<string.h>

char text[]="A nut for a jar of tuna";
int txtposition,txtlength;

void main()
{
    clrscr();
    txtlength=Strlen(text);

    for(txtposition=0; txtposition<=txtlength;txtposition++)
     { 
         cout<<text[txtposition]; 
     }
getch();
}

How to rewrite this program so that it print the value of text in reverse order?


Solution

  • #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    
    char text[]="A nut for a jar of tuna";
    int txtposition,txtlength;
    
    void main()
    {
        clrscr();
        txtlength=Strlen(text);
    
        for(txtposition= txtlength-1 ; txtposition>=0;txtposition--)
         { 
             cout<<text[txtposition]; 
         }
        getch();
    }