Write a recursive string valued function replace that accept
Write a recursive, string -valued function, replace, that accepts a string and returns a new string consisting of the original string with each blank replaced with an asterisk (*).
Answer must be in C.
Solution
// C code replace space with star in string
#include <stdio.h>
#define MAX_SIZE 1000
// replace function
void replacefunction(char * string, char to_replace, char replace_With)
{
static int i = 0;
// run till end of string
if(*string == \'\\0\')
{
return;
}
// if space is found , replace with star
if(*string == to_replace)
{
*string = replace_With;
}
// increment string to next index
string = string+1;
// recursively call function
replacefunction(string, to_replace,replace_With);
}
int main()
{
char string[MAX_SIZE], to_replace, replace_With;
printf(\"Enter string: \");
scanf(\"%[^\ ]%*c\",string);
to_replace = \' \';
replace_With = \'*\';
printf(\"\ String before replacing: %s\ \", string);
replacefunction(string, to_replace, replace_With);
printf(\"String after replacing: %s\ \", string);
return 0;
}
/*
output:
Enter string: my name is alex hales.
String before replacing: my name is alex hales.
String after replacing: my*name*is*alex*hales.
*/

