C program to replace all occurrences of a character in a string

Category: C Program

Learn how to write a C program that replaces every instance of a specified character in a given string with another character. Includes code example and step-by-step explanation.

In this article, we will explore how to write a C program to replace all occurrences of a specific character in a string with another character. This is a common task in text processing where you may need to update strings based on specific requirements.

Problem Statement

Given a string and two characters, oldChar and newChar, we need to replace all occurrences of oldChar in the string with newChar.

Approach

To solve this problem, we can follow these steps:

  1. Take the input string from the user.
  2. Take the characters oldChar and newChar from the user.
  3. Traverse the string character by character.
  4. Whenever we encounter oldChar, we replace it with newChar.
  5. Print the modified string.

Write a C program to replace all occurrences of a character in a string

Here is a simple C program to achieve this:

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    char oldChar, newChar;

    // Taking string input from the user
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Removing the newline character added by fgets
    str[strcspn(str, "\n")] = '\0';

    // Taking the old character and new character inputs from the user
    printf("Enter the character to be replaced: ");
    scanf("%c", &oldChar);
    getchar();  // To consume the newline character left by scanf

    printf("Enter the new character: ");
    scanf("%c", &newChar);

    // Replacing all occurrences of oldChar with newChar
    int length = strlen(str);
    for (int i = 0; i < length; i++) {
        if (str[i] == oldChar) {
            str[i] = newChar;
        }
    }

    // Printing the modified string
    printf("Modified string: %s\n", str);

    return 0;
}

Output

Enter a string: learn programming at ProCoding
Enter the character to be replaced: a
Enter the new character: X
Modified string: leXrn progrXmming Xt ProCoding

Explanation of the Program

  1. Header Files:

    #include <stdio.h>
    #include <string.h>
    
    • stdio.h is included for standard input and output functions.
    • string.h is included for string handling functions like strlen and strcspn.
  2. Replaces all occurrences of character:

    // Replacing all occurrences of oldChar with newChar
    int length = strlen(str);
    for (int i = 0; i < length; i++) {
        if (str[i] == oldChar) {
            str[i] = newChar;
        }
    }
    
    • The main function handles user input and output.
    • It reads the input string and characters from the user, replaces all occurrences of oldChar with newChar, and prints the modified string.

Example Usage

If the user inputs:

  • String: "hello world"
  • Character to be replaced: 'o'
  • New character: 'a'

The program will output:

Modified string: "hella warld"

Recommended Posts