Given a string, compute recursively a new string where all the lowercase 'x' chars have been moved to the end of the string.
For example:
endX("xxre") → "rexx"
endX("xxhixx") → "hixxxx"
endX("xhixhix") → "hihixxx"
Note:
Refer to the problem requirements.
Function specification:
char* endx(char *str)
Input and Output Format:
Input consists of a single line corresponding to the string and the output consists of a single string where all the lowercase 'x' chars have been moved to the end of the string.
Refer sample input and output for formatting specifications.
Sample Input and Output:
Enter the string
xxhixx
hixxxx
Implementation of the above problem:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char * endx(char *s)
{
if(*(s+1)=='\0')
{
return s;
}
else
{
if(*s=='x')
{
int size=strlen(s);
char *s1=(char*)malloc(size*(sizeof(char)));
strcpy(s1,s+1);
strcpy(s, s1);
s=endx(s);
s[size-1]='x';
s[size]='\0';
return s;
}
return endx(s+1)-1;
}
}
int main()
{
char *s, *d;
printf("Enter the string\n");
s=(char*)malloc(1000*(sizeof(char)));
scanf("%[^\n]%*c", s);
d=endx(s);
printf("%s\n", d);
return 0;
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: