/*Write a function strreplace(s,chr,repl_chr) which will replace each occurrences of character chr with the character repl_chr in the string s. The function returns the number of replacements.*/
int strreplace(char [],char,char);
main()
{
char s[160],c,r;
int i;
clrscr();
printf("Enter any string \(word or sentence\).\n");
gets(s);
printf("\nYou enter the following string:\n");
puts(s);
printf("\nEnter the character you want to replace. -> ");
c=getche();
printf("\nEnter the character you want to insert. -> ");
r=getche();
printf("\nYou entered \"%c\" to replace with \"%c\".",c,r);
i=strreplace(s,c,r);
printf("\nAfter replacement, new string is :\n");
puts(s);
printf("\nThe total number of replacements is %d.",i);
getch();
}
int strreplace(char s[],char chr,char repl_chr)
{
int c=0,i;
for(i=0;s[i]!='\0';i++)
{
if(s[i]==chr)
{
s[i]=repl_chr;
c++;
}
}
return c;
}
Output:
Click Here
int strreplace(char [],char,char);
main()
{
char s[160],c,r;
int i;
clrscr();
printf("Enter any string \(word or sentence\).\n");
gets(s);
printf("\nYou enter the following string:\n");
puts(s);
printf("\nEnter the character you want to replace. -> ");
c=getche();
printf("\nEnter the character you want to insert. -> ");
r=getche();
printf("\nYou entered \"%c\" to replace with \"%c\".",c,r);
i=strreplace(s,c,r);
printf("\nAfter replacement, new string is :\n");
puts(s);
printf("\nThe total number of replacements is %d.",i);
getch();
}
int strreplace(char s[],char chr,char repl_chr)
{
int c=0,i;
for(i=0;s[i]!='\0';i++)
{
if(s[i]==chr)
{
s[i]=repl_chr;
c++;
}
}
return c;
}
Output:
No comments:
Post a Comment