How to reverse a string using a recursive function, without
swapping or using an extra memory?




Answers were Sorted based on User's Feedback



How to reverse a string using a recursive function, without swapping or using an extra memory?..

Answer / vinay tiwari

void reverse(char *,int b);
void main()
{
char a[26];
int len;
clrscr();
printf("enter string ");
gets(a);
len=strlen(a);
reverse(a,len);
getch();
}
void reverse(char * a,int len)
{
if(len==0)
printf("%c",a[len]);
else
{
printf("%c",a[len]);
reverse(a,len-1);
}
}

Is This Answer Correct ?    177 Yes 61 No

How to reverse a string using a recursive function, without swapping or using an extra memory?..

Answer / d g patel

/* Following code does as intended */
#include <stdio.h>

#define REVERSE_STRING(X) Rstring(X, *(X), strlen(X)-1)

void Rstring( char *str, char c, int index )
{
if( index != 0 )
Rstring( str, *(str+(strlen(str))-index),
index-1);
*(str+index) = c;
}

int main( void )
{
char str[] = "Dharmendra Patel";
printf("Actual string is [%s]\n", str);
REVERSE_STRING(str);
printf("Reversed string is [%s]\n", str);
return 0;
}

Is This Answer Correct ?    92 Yes 46 No

How to reverse a string using a recursive function, without swapping or using an extra memory?..

Answer / prakash

Another version that actually reverses the string...

#include <stdio.h>

char *reverse(char *sstr, char *str, char c)
{
if (*str == '\0')
return sstr;

sstr = reverse(sstr, str+1, *(str+1));

*sstr = c;

return (sstr+1);
}

int main()
{
char str[100];

printf("Enter the string: ");
scanf("%s", str);

reverse(str, str, *(str + 0));
printf("Reversed string: %s\n", str);

return 1;
}

Is This Answer Correct ?    25 Yes 11 No




How to reverse a string using a recursive function, without swapping or using an extra memory?..

Answer / mahesh auti

#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(void)
{

char str1[] = "Mahesh";
char str2[80], *p1, *p2;

clrscr();

p1 = str1 + strlen(str1) - 1;

p2 = str2;

while(p1 >= str1)
*p2++ = *p1--;

*p2 = '\0';

printf("%s %s", str1, str2);

getch();

return 0;
}

Is This Answer Correct ?    26 Yes 25 No

How to reverse a string using a recursive function, without swapping or using an extra memory?..

Answer / siva kumar

void reverse_string(char *string) {

static int start_index = 0;
static int end_index = strlen(string) - 1;

if (start_index <= end_index) {
char temp = string[end_index];
string[end_index] = string[start_index];
string[start_index] = temp;
start_index++;
end_index--;
reverse_string(string);
}
}

Is This Answer Correct ?    11 Yes 10 No

How to reverse a string using a recursive function, without swapping or using an extra memory?..

Answer / osama al-ahmad

#include <iostream>
#include < string >
using namespace std;
int i=0;
int z=0;
int count=0;
int Length(string name)
{
if (name[i] == '\0')
return count;
else
{
count++;
i++;
return Length(name);
}

}
char Print_B(string name)
{
if (z == Length(name))
return name[z];
else
{
z++;
cout<<name[count];
count--;
return Print_B(name);
}
}
void main()
{
string name;
cin>>name;
cout<<"Name : ";
Length(name);
cout<<Print_B(name);
}

Is This Answer Correct ?    4 Yes 3 No

How to reverse a string using a recursive function, without swapping or using an extra memory?..

Answer / bret

void revDisplay (char a[])
{

if(strlen(a) != 0)
{
revDisplay(a+1);
cout << a[0];
}
}

Is This Answer Correct ?    6 Yes 5 No

How to reverse a string using a recursive function, without swapping or using an extra memory?..

Answer / cmp

#include <stdio.h>
void print(char *s,int size){
if(size<0)
printf(" ");
else

{
printf("%c",*(s+size-1));
print(s,size-1);
}
}

void main(){
char *s={"aliveli"};
int size=0,i=0;

while(*(s+i)!='\0'){
size++;
i++;
}
print(s,size);
}

Is This Answer Correct ?    6 Yes 5 No

How to reverse a string using a recursive function, without swapping or using an extra memory?..

Answer / sravani

#include<stdio.h>
void strrev(char *);
main()
{
char s1[10];
printf("enter the string:");
scanf("%s",s1);
strrev(s1);
}
void strrev(char *p)
{
int i,j;
i=0,j=strlen(p)-1;
char temp;
while(i<j)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
i++;
j--;
strrev(s1);
}

Is This Answer Correct ?    5 Yes 5 No

How to reverse a string using a recursive function, without swapping or using an extra memory?..

Answer / ozzy

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

int recreverse (char szStr[], int nSize, int i)
{
// printf ("\n %d - %s ", i, szStr);

if (i < nSize /2)
{
szStr[i] ^= szStr[nSize -(i + 1)];
szStr[nSize -(i + 1)] ^= szStr[i];
szStr[i] ^= szStr[nSize -(i + 1)];

recreverse (szStr, nSize, ++i);
}
else
return;



}

int main()
{
char szStr[256];
int nSize,i;
char cChar;
// int nHash[26] = {0};
// char szDict[26] ="abcdefghijklmnopqrstuvwxyz";

printf("\n Enter the character : ");
// scanf("%s,", szStr);
gets(szStr);
nSize = strlen (szStr);
printf ("\n string %s - %d \n", szStr, nSize);

recreverse (szStr, nSize, 0);


printf ("\n Reverse <<%s>> \n", szStr);

}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer



More C Interview Questions

why do we use pointer instead directly acessing the data?

2 Answers  


What are compound statements?

0 Answers  


What is indirect recursion? give an example?

4 Answers  


char p="data"; printf(p);

2 Answers  


how does a general function , that accepts an array as a parameter, "knows" the size of the array ? How should it define it parameters list ?

0 Answers  






my project name is adulteration of chille powder.how can i explain it to the hr when he asks me about the project?

0 Answers  


Is it possible to run a c program without using main?If yes HOW??

13 Answers   Wipro,


we compile c program in 32 processor and 64 bit processor .exe file is created in both the processors. if we want to run .exe file in 64 bit processor which is created in 32 bit processor. is that .exe file is run or not if it is not run why?

4 Answers   Wipro, HP,


Why is conio.h not required when we save a file as .c and use clrscr() or getch() ?

2 Answers  


When do you say that a digraph is acyclic A)if and only if its first search does not have back arcs B)a digraph is acyclic if and only if its first search does not have back vertices C)if and only if its first search does not have same dfnumber D)None of these

1 Answers   Accenture, IBM,


what does the following code do? fn(int n,int p,int r) { static int a=p; switch(n){ case 4:a+=a*r; case 3:a+=a*r; case 2:a+=a*r; case 1:a+=a*r; } } a.computes simple interest for one year b.computes amount on compound interest for 1 to 4 years c.computes simple interest for four year d.computes compound interst for 1 year

7 Answers   TCS,


What will be the result of the following C language program? main() { int a = 0; int b = 20; char x = 1; char y = 10; if(a,b,x,y) printf("Welcome"); }

1 Answers  






Categories