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
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: