write a program to reverse a string with/with out reverse() method?
write a program to reverse a string with/with out reverse() method?
program
REVERSE A STRING
#include<stdio.h>
#include<string.h>
main()
{
char str[50],revstr[50];
int i=0,j=0;
printf("Enter the string to be reversed : ");
scanf("%s",str);
for(i=strlen(str)-1;i>=0;i--)
{
revstr[j]=str[i];
j++;
}
revstr[j]='\0';
printf("Input String : %s",str);
printf("\nOutput String : %s",revstr);
getch();
}
to reverse a String using reverse() in StringBuffer
import java.lang.String;
import java.io.*;
class SR{
public static void main(String a[])throws IOException{
String Original;
String Reverse1;
System.out.println("Enter a string ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Original=br.readLine();
System.out.println("Original String" + '\t' +Original);
System.out.println("*************************");
StringBuffer sbr=new StringBuffer(Original);
StringBuffer sbr1=sbr.reverse();
Reverse1=sbr1.toString();
System.out.println("Reverse of OriginalString" +'\t' + Reverse1);
}
}
This is a prg to reverse a String with reverse() of StringBuffer and it works fine
import java.lang.String;
import java.io.*;
class SR{
public static void main(String a[])throws IOException{
String Original;
String Reverse1;
System.out.println("Enter a string ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Original=br.readLine();
System.out.println("Original String" + '\t' +Original);
System.out.println("*************************");
StringBuffer sbr=new StringBuffer(Original);
StringBuffer sbr1=sbr.reverse();
Reverse1=sbr1.toString();
System.out.println("Reverse of OriginalString" +'\t' + Reverse1);
}
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
clrscr();
char str[10];
char ch;
cout<<"Enter a string: ";
cin>>str;
int l=strlen(str);
for(int i=0;i<=l/2;i++)
{
ch=str[i];
str[i]=str[l-1];
str[l-1]=ch;
l--;
}
cout<<"\n"<<str<<endl;
getch();
return 0;
}
//With reverse()
-----------------------
class ResString{
public static void main(String args[]){
String str=args[0];
System.out.println("BEFORE REVERSE===="+str);
StringBuffer strbr=new StringBuffer(str);
strbr.reverse();
System.out.println("After REVERSE===="+strbr);
}
}
//without reverse()
----------------------
lass ResString
{
public static void main(String args[])
{
for(int i=args.length-1;i>=0;i--)
{
for(int j=args[i].length()-1;j>=0;j--)
{
System.out.print(args[i].charAt(j));
}
System.out.print(" ");
}
}
}
accept the input through single link list..and jusst print the list.the answer wil b the reverse of string