#1  
26th August 2012, 09:07 AM
Unregistered
Guest
 
Posts: n/a

How to write a Program to reverse a string?


write a program to reverse a string




  #2  
27th August 2012, 10:41 PM
suvendum14
Senior Member
 
Join Date: Aug 2012
Location: west bengal
Posts: 255
Default 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, 12:29 PM
BISWARUPYOGI
Senior Member+++++
 
Join Date: May 2011
Location: India
Posts: 38,947
Default Re: How to write a Program to reverse a string?

Quote:
Originally Posted by Unregistered View Post
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 (&quot;Enter Your Name: &quot;
scanf (&quot;%s&quot;,word);

i = strlen(word)-1;

while (i >= 0)
{
printf (&quot;%c&quot;,word[i]);
i--;
}
getch();
}

Using Pointer:-

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
char *a;
clrscr();
printf(&quot;enter a string&quot;
scanf(&quot;%s&quot;,a);
i=0;
while(*(a+i)!='\0')
i++;
for(j=i-1;j>=0;j--)
printf(&quot;%c&quot;,*(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(&quot;\nString before reverse:
&quot;+string);
System.out.println(&quot;String after reverse:
&quot;+reverse);
}
}

.
  #4  
15th September 2012, 08:24 PM
mustafa tahasildar
Senior Member++
 
Join Date: Aug 2012
Location: SINDHANOOR
Posts: 1,441
Default Re: How to write a Program to reverse a string?

Quote:
Originally Posted by Unregistered View Post
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
Reply With Quote
Do you have any question? or have anything to say?





Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
vBulletin Optimisation by vB Optimise.
Please also check: