
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to replace all occurrence of a character in a string
Enter a string at run time and read a character to replace at console. Then, finally read a new character that has to be placed in place of an old character where ever it occurs inside the string.
Program1
Following is the C program to replace all occurrence of a character −
#include <stdio.h> #include <string.h> int main(){ char string[100], ch1, ch2; int i; printf("enter a string : "); gets(string); printf("enter a character to search : "); scanf("%c", &ch1); getchar(); printf("enter a char to replace in place of old : "); scanf("%c", &ch2); for(i = 0; i <= strlen(string); i++){ if(string[i] == ch1){ string[i] = ch2; } } printf("
the string after replace of '%c' with '%c' = %s ", ch1, ch2, string); return 0; }
Output
When the above program is executed, it produces the following result −
enter a string: Tutorials Point enter a character to search: i enter a char to replace in place of old: % the string after replace of 'i' with '%' = Tutor%als Po%nt enter a string: c programming enter a character to search: m enter a char to replace in place of old: $ the string after replace of 'm' with '$' = c progra$$ing
Program2
Following is the C program to replace at first occurrence −
#include <stdio.h> #include <string.h> int main(){ char string[100], ch1, ch2; int i; printf("enter a string : "); gets(string); printf("enter a character to search : "); scanf("%c", &ch1); getchar(); printf("enter a char to replace in place of old : "); scanf("%c", &ch2); for(i = 0; string[i]!='\0'; i++){ if(string[i] == ch1){ string[i] = ch2; break; } } printf("
the string after replace of '%c' with '%c' = %s ", ch1, ch2, string); return 0; }
Output
When the above program is executed, it produces the following result −
Run 1: enter a string: Tutorial Point enter a character to search: o enter a char to replace in place of old: # the string after replace of 'o' with '#' = Tut#rial Point Run 2: enter a string: c programming enter a character to search: g enter a char to replace in place of old: @ the string after replace of 'g' with '@' = c pro@ramming
- Related Articles
- String function to replace nth occurrence of a character in a string JavaScript
- C Program to find minimum occurrence of character in a string
- C Program to find maximum occurrence of character in a string
- Java Program to replace all occurrences of a given character in a string
- C# Program to find number of occurrence of a character in a String
- Replace first occurrence of a character in Java
- C# Program to replace a special character from a String
- Java Program to replace all occurrences of a given character with new character
- Java program to check occurrence of each character in String
- C# program to replace n-th character from a given index in a string
- C# program to replace all spaces in a string with ‘%20’
- Create a polyfill to replace nth occurrence of a string JavaScript
- Java program to count the occurrence of each character in a string using Hashmap
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- Java Program to Replace the Spaces of a String with a Specific Character

Advertisements