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
What will the preprocessor do for a program?
Implement bit Array in C.
how logic is used
Describe the order of precedence with regards to operators in C.
Is python a c language?
What is difference between structure and union with example?
What functions are used in dynamic memory allocation in c?
Can we use any name in place of argv and argc as command line arguments?
differentiate built-in functions and user – defined functions.
Can we access the array using a pointer in c language?
How can you invoke another program from within a C program?
What is dynamic dispatch in c++?
What is structure and union in c?
Are bit fields portable?
formula to convert 2500mmh2o into m3/hr