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

Write program to remove duplicate in an array?

601


What is the time and space complexities of merge sort and when is it preferred over quick sort?

676


What is the use of getch ()?

636


a linearly ordered set of data elements that have the same structure and whose order is preserved in storage by using sequential allocation a) circular b) ordinary c) array d) linear list

632


What is the difference between arrays and pointers?

634






What is the difference between Printf(..) and sprint(...) ?

789


What is the use of a semicolon (;) at the end of every program statement?

774


Is it better to use malloc() or calloc()?

649


What is an example of structure?

588


c language interview questions & answer

1463


Can you return null in c?

597


How does struct work in c?

611


Why do we need volatile in c?

744


What is Dynamic memory allocation in C? Name the dynamic allocation functions.

559


i want to switch my career from quailty assurance engineering to development kindly guide me from which programming language its better for me to start plz refer some courses or certifications too i have an experience of 1.5 yrs in QA field.Kindly guide me

1480