How to reverse a string using a recursive function, without
swapping or using an extra memory?
Answer Posted / mahesh auti
#include<stdio.h>
#include <string.h>
char * reverse (char *); //function prototype
int length(char *); //function prptotype
void main()
{
int i;
char *str, *rev;
clrscr();
gets(str);
strcpy(rev,reverse(str));
printf("Original %s Reverse %s", str, rev);
free(str);
free(rev);
getch();
}
int length(char *s)
{
int i;
for (i=0; *(s+i)!='\0' ; ++i);
return i;
}
char *reverse(char *s)
{
char *t;
int i, n;
n=length(s);
for (i=0; i<n; ++i)
{
*(t+i)=*(s+n-1-i);
}
*(t+i)='\0';
printf("\nOUT: %s\n", t);
return t;
}
| Is This Answer Correct ? | 17 Yes | 27 No |
Post New Answer View All Answers
Explain why can’t constant values be used to define an array’s initial size?
Can a pointer be volatile in c?
Explain why c is faster than c++?
How can I check whether a file exists? I want to warn the user if a requested input file is missing.
Why does notstrcat(string, "!");Work?
What is property type c?
What is function pointer c?
Why do we use return in c?
In c programming typeing to occupy the variables in memory space. if not useing the variable the memory space is wasted.ok, how to avoid the situation..? (the variable is used & notused)
Write a program to check palindrome number in c programming?
The postoder traversal is 7,14,3,55,22,5,17 Then ur Inorder traversal is??? please help me on this
How can I do peek and poke in c?
Program to find the sum of digits of a given number until the sum becomes a single digit. (e.g. 12345=>1+2+3+4+5=15=>1+5=6)
Write a program to find the biggest number of three numbers in c?
Difference between constant pointer and pointer to a constant.