"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Thursday, February 18, 2010

Write a program to reverse a string (i) using another array, (ii) without another array.


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

void main()
{

int str[50], rev[50];

cout<< "\n Enter a string :";

 gets(str);
int l = strlen(str);

/* Reverse String using another array */
for( int i = l-1, k = 0 ; i>= 0 ; i--, k++)
rev[k] = str[i];

 rev[k] = '\0';

cout<< "\n Original String is "<< str;
cout<< "\n Reverse String is "<< rev ;
cout<< "\n Reverse String is ";

/* Reverse String without using another array */
for(  i = l-1 ; i>=0 ; i--)
 cout<< str[i];

 getch();

}

2 comments:

  1. #include
    #include
    #include
    void main()
    {
    char a[40],b[40];
    int i,l;
    cout<<"\nenter ur string";
    gets(a);
    l=strlen(a);
    for(i=0;i<l;i++)
    {
    b[i]=a[l-(i+1)];
    }
    for(i=0;i<l;i++)
    {
    cout<<b[i];
    }}

    ReplyDelete

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.