#1
26th August 2012, 10:07 AM
|
|||
|
|||
How to write a Program to reverse a string?
write a program to reverse a string
|
#2
27th August 2012, 11:41 PM
|
|||
|
|||
Re: How to write a Program to reverse a string?
Hi,
being programmer i am giving you these...I think this will work.. Here are some sample C programs to do the same.. Method1 (Recursive) #include static char str[]="STRING TO REVERSE"; int main(int argc, char *argv) { printf("\nOriginal string : [%s]", str); // Call the recursion function reverse(0); printf("\nReversed string : [%s]", str); return(0); } int reverse(int pos) { // Here I am calculating strlen(str) everytime. // This can be avoided by doing this computation // earlier and storing it somewhere for later use. if(pos<(strlen(str)/2)) { char ch; // Swap str[pos] and str[strlen(str)-pos-1] ch = str[pos]; str[pos]=str[strlen(str)-pos-1]; str[strlen(str)-pos-1]=ch; // Now recurse! reverse(pos+1); } } Method2 #include #include #include void ReverseStr ( char *buff, int start, int end ) { char tmp ; if ( start >= end ) { printf ( "\n%s\n", buff ); return; } tmp = *(buff + start); *(buff + start) = *(buff + end); *(buff + end) = tmp ; ReverseStr (buff, ++start, --end ); } int main() { char buffer[]="This is Test"; ReverseStr(buffer,0,strlen(buffer)-1); return 0; } Method3 public static String reverse(String s) { int N = s.length(); if (N <= 1) return s; String left = s.substring(0, N/2); String right = s.substring(N/2, N); return reverse(right) + reverse(left); } Method4 for(int i = 0, j = reversed.Length - 1; i < j; i++, j--) { char temp = reversed[i]; reversed[i] = reversed[j]; reversed[j] = temp; } return new String(reversed); Method5 public static String reverse(String s) { int N = s.length(); String reverse = ""; for (int i = 0; i < N; i++) reverse = s.charAt(i) + reverse; return reverse; } Method6 public static String reverse(String s) { int N = s.length(); char[] a = new char[N]; for (int i = 0; i < N; i++) a[i] = s.charAt(N-i-1); String reverse = new String(a); return reverse; } Regards...~$uVenDu~ |
#3
2nd September 2012, 01:29 PM
|
|||
|
|||
Re: How to write a Program to reverse a string?
Write a Program to reverse a string !!!
C Program:- # include <stdio.h> # include <conio.h> # include <string.h> void main () { char word[20]; int i; printf ("Enter Your Name: " scanf ("%s",word); i = strlen(word)-1; while (i >= 0) { printf ("%c",word[i]); i--; } getch(); } Using Pointer:- #include<stdio.h> #include<conio.h> void main() { int i,j; char *a; clrscr(); printf("enter a string" scanf("%s",a); i=0; while(*(a+i)!='\0') i++; for(j=i-1;j>=0;j--) printf("%c",*(a+j)); getch(); } Java Program:- public class StringReverseExample { public static void main(String[] args) { String string=args[0]; String reverse = new StringBuffer(string). reverse().toString(); System.out.println("\nString before reverse: "+string); System.out.println("String after reverse: "+reverse); } } . |
#4
15th September 2012, 09:24 PM
|
|||
|
|||
Re: How to write a Program to reverse a string?
Method1 (Recursive)
#include static char str[]="STRING TO REVERSE"; int main(int argc, char *argv) { printf("\nOriginal string : [%s]", str); // Call the recursion function reverse(0); printf("\nReversed string : [%s]", str); return(0); } int reverse(int pos) { // Here I am calculating strlen(str) everytime. // This can be avoided by doing this computation // earlier and storing it somewhere for later use. if(pos<(strlen(str)/2)) { char ch; // Swap str[pos] and str[strlen(str)-pos-1] ch = str[pos]; str[pos]=str[strlen(str)-pos-1]; str[strlen(str)-pos-1]=ch; // Now recurse! reverse(pos+1); } } Method2 #include #include #include void ReverseStr ( char *buff, int start, int end ) { char tmp ; if ( start >= end ) { printf ( "\n%s\n", buff ); return; } tmp = *(buff + start); *(buff + start) = *(buff + end); *(buff + end) = tmp ; ReverseStr (buff, ++start, --end ); } int main() { char buffer[]="This is Test"; ReverseStr(buffer,0,strlen(buffer)-1); return 0; } Method3 public static String reverse(String s) { int N = s.length(); if (N <= 1) return s; String left = s.substring(0, N/2); String right = s.substring(N/2, N); return reverse(right) + reverse(left); } Method4 for(int i = 0, j = reversed.Length - 1; i < j; i++, j--) { char temp = reversed[i]; reversed[i] = reversed[j]; reversed[j] = temp; } return new String(reversed); Method5 public static String reverse(String s) { int N = s.length(); String reverse = ""; for (int i = 0; i < N; i++) reverse = s.charAt(i) + reverse; return reverse; } Method6 public static String reverse(String s) { int N = s.length(); char[] a = new char[N]; for (int i = 0; i < N; i++) a[i] = s.charAt(N-i-1); String reverse = new String(a); return reverse; } Source: http://entrance-exam.net/forum/general-discussion/how-write-program-reverse-string-918667.html#ixzz5f5e8AQVI |
|