c program to replace characters in string

c program to replace characters in string and count occurrences

For writing a c program to replace characters in string and count occurrences  we need to 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. Place the source code of this function in a file  named strreplace.c
strreplace(s, chr, repl_chr) function replace characters in string and returns the number of replacements.

#include<stdio.h>
void strreplace(char *,char,char);
int main()
{
char oldch,newch;
char s[10];
printf(“\n–Replace characters in string and count number of replacements—“);
printf(“\nEnter a string: “);
scanf(“%s”, s);
printf(“\nEnter character to be replaced: “);
scanf(“%c”, &oldch);
printf(“\nEnter replacement character: “);
scanf(“%c”, &newch);
printf(“\n newch %c\t”,newch);
printf(“old ch %c\t\n”,oldch);

printf(“Before:\t%s\n”, s);
strreplace(s,oldch,newch);
printf(“After:\t%s\n”, s);
return 0;
}

void strreplace(char s[], char chr, char rchr)
{
int i=0;
while(s[i]!= ‘\0’)
{
if(s[i]==chr)
{
s[i]=rchr;
}
i++;
}
printf(“\nAfter:\t%s\n”, s);
}

Other programs:

Static variable scope
Register variable scope
External variable scope
Automatic variable scope
amicable number
Prime number
Perfect number
Palindrome number
Strong number
Find Factorial 
Biggest Number
Average of n numbers
convert decimal number to octal
decimal number to hexadecimal
decimal number to binary
simple interest
compound interest
Add sparce matrices
Add matrices
 file copying
Palindrome string
Reverse string
String length
Replace characters in string
substring or not using arrays
First occurrence of character in string
Last occurrence of character in string
Convert to uppercase
Convert to lowercase
Concatenate strings 
Compare strings
Generate Employee payslips
Count students scoring marks>50%
convert weight into grams

 

Leave a Reply

Your email address will not be published. Required fields are marked *