Program for Replacing a character.

#include <stdio.h>
#include <conio.h>

void main()
{
int replace_char(char *,char,char);
char *str;
char old_char,new_char;
int r;
clrscr();
printf("\n\n\n\t\t\t Program for Replacing a character");

printf("\n\n\n\t\t\t Enter a line of text :");
printf("\n\n\t >> ");

gets(str);

printf("\n\n\t Enter the Character to find = ");
flushall();
scanf("%c",&old_char);

printf("\n\n\t Enter the Character to be replaced = ");
flushall();
scanf("%c",&new_char);

r=replace_char(str,old_char,new_char);

printf("\n\n\n\t\t\t Line after replacing the characters :");
printf("\n\n\t >> %s",str);
printf("\n\n\t Total character replaced = %d",r);
getch();
}

int replace_char(char *s,char o,char n)
{
int i,f=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]==o)
{
s[i]=n;
f++;
}
}
return(f);
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin