C Program to replace first occurrence of a character in a string
Learn how to write a C program to replace the first 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 first occurrence of a specified character in a given string with another character. This task is useful in various text processing and data manipulation applications.
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 first 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:
- Read the input string from the user.
- Read the character to be replaced and the replacement character from the user.
- Find the first occurrence of the specified character in the string.
- Replace the character at the found position with the replacement character.
- Print the modified string.
Detailed Steps
- Read the input string:
- Use standard input functions to get the string from the user.
- Read the character to be replaced and the replacement character:
- Use standard input functions to get the characters from the user.
- Find the first occurrence of the character:
- Traverse the string to find the index of the first occurrence of the specified character.
- Replace the character:
- If the character is found, replace it with the new character.
- Print the modified string:
- Print the resulting string after the character has been replaced.
Write a C Program to replace first 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 first occurrence of the character to replace
for (int i = 0; i < strlen(str); 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 first 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: a
Enter the replacement character: X
String after replacing first occurrence of 'a' with 'X': leXrn programming at ProCoding
Explanation of the Program
- 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()
.
- The program uses
- 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 byscanf()
before reading the second character.
- The program uses
- Finding the First Occurrence:
- A
for
loop iterates over the string to find the first index where the specified character occurs. If found, the index is stored in the variableindex
.
- A
- Replacing the Character:
- If the character is found (
index != -1
), the program replaces the character at the found index with the replacement character.
- If the character is found (
- 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.