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
Is there a way to compare two structure variables?
What is the difference between single charater constant and string constant?
What is boolean in c?
What is the difference between char array and char pointer?
What does a pointer variable always consist of?
What does stand for?
how can use subset in c program and give more example
What are the advantages of using Unions?
What is the difference between %d and %i?
Can you please explain the difference between strcpy() and memcpy() function?
What does %c do in c?
What is the difference between abs() and fabs() functions?
How to delete a node from linked list w/o using collectons?
What is c mainly used for?
Write a code to determine the total number of stops an elevator would take to serve N number of people.