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 / aditi parab

#include <stdio.h>
int main ()
{
int i,j;
char a[10];
char temp;
//clrscr (); // only works on windows
gets(a);
for (i=0;a[i]!='\0';i++);
i--;
for (j=0;j <= i/2 ;j++)
{
temp = a[j];
a[j] = a[i-j];
a[i-j] = temp;
}
printf("%s",a);
return(0);
}

Is This Answer Correct ?    1 Yes 1 No

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

Answer / sasi

#include<stdio.h>
#include<conio.h>
void main()
{
printf("\n i dont know");
}
getch();

Is This Answer Correct ?    0 Yes 0 No

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

Answer / vishnu

Try this ...

#include <stdio.h>
#include <stdlib.h>

int Rev (char *s, char *b)
{
int i ;
char c ;

if (*s == '\0')
{
return 0 ;
}
c = *s ;
i = Rev (s + 1, b) ;
b[i] = c ;
return i+1 ;
}

int main ()
{
int end ;
char str[] = "Billie jean is not my lover - MJ" ;

end = Rev (str, str) ;
str[end] = '\0' ;
printf ("Now [%s]\n", str) ;
exit (0) ;
}

Is This Answer Correct ?    1 Yes 1 No

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

Answer / rahul kumar

/* Program to reverse any string input by the user without
using library function strlen( );*/

#include <stdio.h>
#include<conio.h>
void main()
{
char a[]={"sixaN: you are with us or against us"};
int i,len=0;
char *b;
clrscr();
b=a;
while(*b!='\0')
{
len++;
b++;
} //counting lenght of string

for(i=len;i>-1;i--)
printf("%c",a[i]); //printing charachters in reverse
getch();


Output :

su tsniaga ro su htiw era uoy :Naxis

Is This Answer Correct ?    0 Yes 0 No

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

Answer / aravind

#Include<stdio.h>
void display(char*)
void main()
{
char str[]= "Aravind"
disply(str)
}
void display(char *p)
{
static int i=1;
if(*p=='\0')
{
display(p+i)
i++
}
printf("%c",*p)
}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / prakash

#include <stdio.h>

void reverse(char *str)
{
if (*str == '\0')
return;

reverse(str+1);

printf("%c", *str);
}

int main()
{
char str[50];

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

printf("Reversed string: ");
reverse(str);
printf("\n");

return 1;
}

Is This Answer Correct ?    23 Yes 24 No

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

Answer / right

char* reverseStringR(char* string){
if(string[0] && !string[1])
return string;

char first = string[0];

reverseStringR(string+1);
size_t length_rest = strlen(string+1);
memmove(string, string+1, length_rest);
string[length_rest] = first;

return string;
}

Is This Answer Correct ?    3 Yes 4 No

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

Answer / asif

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

void reverse(char *str)
{
if (*str == '\0')
return;

reverse(str+1);

printf("%c", *str);
}

int main()
{
char str[50];
char *ptr;
printf("Enter the string: ");
//scanf("%s", str);
fgets(str,50,stdin);
ptr = strchr(str,'\n');
*ptr = '\0';

printf("Reversed string: ");
reverse(str);
printf("\n");

return 1;
}

Is This Answer Correct ?    2 Yes 3 No

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

Answer / pritam

/*
reverse string between start and end indexes of a string
*/

void reverse( char* str, int start, int end )
{
if( str && ( start < end ) )
{
*( str + start ) ^= *( str + end ) ^= *( str + start )
^= *( str + end ) ;
reverse( str, ++start, --end );
}
}

int main()
{
char sample[] = "My String!";
reverse( str, 0, strlen( sample )-1 )
}

Is This Answer Correct ?    15 Yes 17 No

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

Answer / mahendra aseri

Reverse a string

void ReverseString (char *String)
{
char *Begin = String;
char *End = String + strlen(String) - 1;
char TempChar = '\0';

while (Begin < End)
{
TempChar = *Begin;
*Begin = *End;
*End = TempChar;
Begin++;
End--;
}
}

Is This Answer Correct ?    24 Yes 27 No

Post New Answer

More C Interview Questions

Explain the difference between the local variable and global variable in c?

0 Answers  


proc() { static i=10; printf("%d",i); } If this proc() is called second time, what is the output?

7 Answers   Hughes,


Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and finally create a clean S2 with the relevant characters deleted.

0 Answers  


c program to input values in a table(using 2D array) and print odd numbers from them

1 Answers  


c program to compute AREA under integral

0 Answers   Infosys,


What is the explanation for the dangling pointer in c?

0 Answers  


a c variable cannot start with a) an alphabet b) a number c) a special symbol d) both b and c above

0 Answers  


If we give two names then this displays the connection between the two people. It is nothing but flames game

1 Answers  


what is difference between overriding and overloading?

1 Answers  


What do you mean by Recursion Function?

0 Answers   Hexaware,


please help me..... please codes and flowchart plz turbo c lang po yan.....please asap response... 3. Make an astrology program. The user types in his or her birthday (month, day, and year as integer), and the program responds with the user’s zodiac sign, horoscope, and other information related to it. If the user’s birth year falls into a leap year, your program should display an appropriate message for it. NOTES: Conditional Statements: it should be with graphics

0 Answers  


Can you write the algorithm for Queue?

0 Answers   College School Exams Tests, TCS,


Categories