Writing a function strreplace(s,chr,repl_chr) to replace the character 'chr' with character 'repl_chr' in a givne string s.
I have done so far with following code. Getting awkward output.
// Program to replace occurence of a character with another character in a string.
#include<stdio.h>
#include<conio.h>
void strreplace(char *,char,char);
int main()
{
char s[10],chr,repl_chr;
printf("\nEnter a string: ");
scanf("%s", &s);
printf("\nEnter character to be replaced: ");
scanf("%s", &chr);
printf("\nEnter replacement character: ");
scanf("%s", &repl_chr);
printf("\nModified string after replacement is: ");
strreplace(s,chr,repl_chr);
getch();
return 0;
}
void strreplace(char s[], char chr, char repl_chr)
{
int i=0;
while(s[i]!='\0')
{
if(s[i]==chr)
{
s[i]=repl_chr;
}
i++;
}
printf("%s",s);
}
Thanx.