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 |
The number of bytes of storage occupied by short, int and long are a) 2, 2 and 4 b) 2, 4 and 4 c) 4, 4 and 4 d) none
Diff between for loop and while loop?
What's the difference between constant char *p and char * constant p?
How macro execution is faster than function ?
Describe the complexity of Binary search, Quicksort and various other sorting and searching techniques..
What is main void in c?
How can you determine the size of an allocated portion of memory?
hello friends what do u mean by BUS ERROR i got this error while i am doing my program in DATA STRUCTURES
Write a C program to multiply tho numbers without using arithmetic operator (+, -, *, /).
What are the advantages and disadvantages of pointers?
in one file global variable int i; is declared as static. In another file it is extern int i=100; Is this valid ?
What is the value of a[3] if integer a[] = {5,4,3,2,1}?