How to reverse a string using a recursive function, with
swapping?
Answers were Sorted based on User's Feedback
Answer / vignesh1988i
#include<stdio.h>
#include<conio.h>
char a1[50]; //GLOABAL VAR.
void reverse(int);
void main()
{
int count=0;
printf("enter the string :");
scanf("%s",a1);
for(int i=0;a1[i]!='\0';i++)
count++;
reverse(count);
getch();
}
void reverse(int count1)
{
char temp;
static int i=0;
if(i<=count1/2)
{
temp=a1[i];
a1[i]=a1[count1-1];
a1[count1-1]=temp;
i++;
reverse(--count1);
}
else
printf("\nthe reversed string is :%s",a1);
}
thank u
| Is This Answer Correct ? | 8 Yes | 1 No |
Answer / kamrul islam
#include<stdio.h>
#include<string.h>
char s1[50];
void reverse();
int main()
{
scanf("%s",s1);
reverse();
return 0;
}
void reverse()
{
char temp;
int n=strlen(s1);
static int i=0;
if (i<n/2)
{
temp=s1[n-i-1];
s1[n-i-1]=s1[i];
s1[i]=temp;
i++;
reverse();
}
else
printf("The reverse string is %s\n",s1);
}
~
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / vignesh1988i
my next logic:::
#include<stdio.h>
#include<conio.h>
void reverse(char*,char*);
void main()
{
char a1[50],*p;
int count=0;
printf("enter the string:");
scanf("%s",a1);
for(int i=0;a[i]!='\0';i++)
count++;
p=a1+(count-1);
reverse(a1,p);
printf("the reversed one is : %s",a1);
getch();
}
void reverse(char *a1,char *p)
{
char temp;
if(a1<=p)
{
temp=*a1;
*a1=*p;
*p=temp;
reverse(++a1,--p);
}
}
thank u
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / nitin
#include<stdio.h>
#include<conio.h>
#include<string.h>
char * reverse (char *);
void main()
{
char p[90],*k;
gets(p);
clrscr();
k=reverse(p);
puts(k);
getch();
}
char * reverse(char *p)
{
char *k="";
if (*p==NULL )
{
return("");
}
else
{
k=reverse(p+1);
}
k[strlen(k)]=*p ;
k[strlen(k)+1]=NULL;
return k;
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / vignesh1988i
the corrected code is:::
#include<stdio.h>
#include<conio.h>
char a1[50]; //GLOABAL VAR.
void reverse(int);
void main()
{
int count=0;
printf("enter the string :");
scanf("%s",a1);
for(int i=0;a1[i]!='\0';i++)
count++;
reverse(count);
getch();
}
void reverse(int count1)
{
char temp;
static int i=0;
if(i!=count)
{
temp=a1[i];
a1[i]=a1[count1-1];
a1[count1-1]=temp;
i++;
reverse(--count1);
}
else
printf("\nthe reversed string is :%s",a1);
}
| Is This Answer Correct ? | 1 Yes | 3 No |
int *a[5] refers to
how to write a c program to print list of fruits in alpabetical order?
What are the different types of control structures?
Combinations of fibanocci prime series
A program to allow an input operand and operator from the operator and read on the display and output operand.
related to rdbms query .
How do I round numbers?
What is the difference between int main and void main?
How can we open a file in Binary mode and Text mode?what is the difference?
errors in computer programmes are called
Convert the following expression to postfix and prefix (A+B) * (D-C)
how much salary u want ? why u join in our company? your domain is core sector why u prefer software ?