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




Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

How will you find a duplicate number in a array without negating the nos ?

1035


find out largest elemant of diagonalmatrix

954


Design a program which assigns values to the array temperature. The program should then display the array with appropriate column and row headings.

1117


What is the use of a ‘’ character?

16


Write a program to generate the Fibinocci Series

23






the maximum length of a character constant can be a) 1 character b) 8 characters c) 256 chaacters d) 125 characters

20


is it possible to create your own header files?

19


WRITE A PROGRAM TO MERGE TWO SORTED ARRAY USING MERGE SORT TECHNIQUE..

930


The program will first compute the tax you owe based on your income. User is prompted to enter income. Program will compute the total amount of tax owed based on the following: Income Tax 0 - $45,000 = 0.15 x income $45,001 - $90,000 = 6750 + 0.20 x (income – 45000) $90,001 - $140,000 = 15750 + 0.26 x (income – 90000) $140,001 - $200,000 = 28750 + 0.29 x (income – 140000) Greater than $200,000 = 46150 + 0.33 x (income – 200000) Dollar amounts should be in dollars and cents (float point numbers with two decimals shown). Tax is displayed on the screen.

407


How arrays can be passed to a user defined function

7


regarding pointers concept

926


.find the output of the following program? char*myfunc(char*ptr) { ptr +=3; return (ptr); } int main() { char*x,*y; x="HELLO"; y=myfunc(x); printf("y = %s ",y); return 0; }

330


Write a C Program That Will Count The Number Of Even And Odd Integers In A Set using while loop

1088


Explain 'bus error'?

1


What are 'near' and 'far' pointers?

1