C Program to replace last occurrence of a character in a string

Category: C Program
Tags: #cprogram#string

Learn how to write a C program to replace the last occurrence of a specified character in a string. This guide includes step-by-step instructions, code implementation, and detailed explanations for beginners in C programming.

In this article, we will discuss how to write a C program that replaces the last occurrence of a specified character in a given string with another character. This operation is useful in various text processing and data manipulation tasks where specific character replacement is required.

Objective

The goal is to create a C program that takes a string, a character to be replaced, and a replacement character as inputs, finds the last occurrence of the specified character in the string, replaces it with the new character, and then prints the modified string.

Approach

To solve this problem, we will:

  1. Read the input string from the user.
  2. Read the character to be replaced and the replacement character from the user.
  3. Find the last occurrence of the specified character in the string.
  4. Replace the character at the found position with the replacement character.
  5. Print the modified string.

Detailed Steps

  1. Read the input string:
    • Use standard input functions to get the string from the user.
  2. Read the character to be replaced and the replacement character:
    • Use standard input functions to get the characters from the user.
  3. Find the last occurrence of the character:
    • Traverse the string to find the index of the last occurrence of the specified character.
  4. Replace the character:
    • If the character is found, replace it with the new character.
  5. Print the modified string:
    • Print the resulting string after the character has been replaced.

Write a C Program to replace last occurrence of a character in a string

Below is the complete C program that implements the above steps:

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

int main() {
    char str[100];
    char char_to_replace, replacement_char;
    int index = -1;

    // Input the string
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character from string if present
    size_t len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';
    }

    // Input the character to be replaced
    printf("Enter the character to replace: ");
    scanf("%c", &char_to_replace);
    getchar();  // To consume the newline character left by scanf

    // Input the replacement character
    printf("Enter the replacement character: ");
    scanf("%c", &replacement_char);

    // Find the last occurrence of the character to replace
    for (int i = len-1; i > 0; i--) {
        if (str[i] == char_to_replace) {
            index = i;
            break;
        }
    }

    // If the character is found, replace it with the new character
    if (index != -1) {
        str[index] = replacement_char;
    }

    // Print the modified string
    if (index != -1) {
        printf("String after replacing last occurrence of '%c' with '%c': %s", char_to_replace, replacement_char, str);
    } else {
        printf("Character '%c' not found in the string.", char_to_replace);
    }

    return 0;
}

Output

Enter a string: learn programming at ProCoding
Enter the character to replace: i
Enter the replacement character: X
String after replacing last occurrence of 'i' with 'X': learn programming at ProCodXng

Explanation of the Program

  1. Input Handling:
    • The program uses fgets() to read a line of text from the user, ensuring that spaces and special characters are included.
    • It removes any trailing newline character added by fgets().
  2. Reading the Characters:
    • The program uses scanf() to read the character to be replaced and the replacement character.
    • A call to getchar() is used to consume the newline character left by scanf() before reading the second character.
  3. Finding the Last Occurrence:
    • A for loop iterates over the string from last to first index, if it encounters the occurrence of character to be replaced then stops loop and save the index to variable value.
  4. Replacing the Character:
    • If the character is found (index != -1), the program replaces the character at the found index with the replacement character.
  5. Output:
    • The modified string is printed if the character was found and replaced. If the character was not found, a message is printed to inform the user.

Recommended Posts