Write a C program to compute recursively a new string where all the adjacent chars are now separated by a "*", for a given string.
For example,
"hello" → "h*e*l*l*o"
Note:
Refer to the problem requirements.
Function specification:
void allstar(char *str)
Input and Output Format:
Input consists of a string.
The output consists of a string.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to the input and the rest corresponds to output.]
Sample Input and Output:
Enter the string
hello
h*e*l*l*o
Implementation of the above problem:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void allstar(char *str)
{
if(strlen(str)<2)
{
return;
}
char *a;
a=(char*)malloc(sizeof(char)*strlen(str));
strcpy(a,str+1);
*(str+1)='*';
strcpy(str+2, a);
allstar(str+2);
return;
}
int main()
{
char *str;
str= (char*)malloc(sizeof(char)*100);
printf(" Enter the string\n");
scanf("%s", str);
allstar(str);
printf("%s\n",str);
return 0;
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: