C Program
#include <stdio.h> #include <string.h> int main() { char str[100]; char oldChar, newChar; printf("Enter a string: "); fgets(str, sizeof(str), stdin); printf("Enter character to replace: "); scanf(" %c", &oldChar); printf("Enter new character: "); scanf(" %c", &newChar); for(int i = 0; str[i] != '\0'; i++) { if(str[i] == oldChar) { str[i] = newChar; } } printf("Modified string: %s", str); return 0; }
C Output
Input: Enter a string: hello world Enter character to replace: l Enter new character: x Output: Modified string: hexxo worxd
C++ Program
#include <iostream> #include <string> using namespace std; int main() { string str; char oldChar, newChar; cout << "Enter a string: "; getline(cin, str); cout << "Enter character to replace: "; cin >> oldChar; cout << "Enter new character: "; cin >> newChar; for(int i = 0; i < str.size(); i++) { if(str[i] == oldChar) { str[i] = newChar; } } cout << "Modified string: " << str; return 0; }
C++ Output
Input: Enter a string: banana Enter character to replace: a Enter new character: o Output: Modified string: bonono
JAVA Program
import java.util.Scanner; public class ReplaceCharacter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a string: "); String str = sc.nextLine(); System.out.print("Enter character to replace: "); char oldChar = sc.next().charAt(0); System.out.print("Enter new character: "); char newChar = sc.next().charAt(0); String result = str.replace(oldChar, newChar); System.out.println("Modified string: " + result); } }
JAVA Output
Input: Enter a string: apple pie Enter character to replace: p Enter new character: q Output: Modified string: aqqle qie
Python Program
str_input = input("Enter a string: ") oldChar = input("Enter character to replace: ") newChar = input("Enter new character: ") result = str_input.replace(oldChar, newChar) print("Modified string:", result)
Python Output
Input: Enter a string: programming Enter character to replace: m Enter new character: n Output: Modified string: pronraning
In-Depth Explanation
Example
If you consider the word "banana" and replace every instance of "a" with "o", then you have "bonono". The reasoning is simple: loop over every character of the string, see if it's the character you are looking to replace, and if so, replace it with the new one. There is an inbuilt function for most programming languages, such as replace(), but it's also essential to know how to do this by hand because on interview questions, you can be asked to do this without using built-in functions.
Real-Life Analogy
Think of it similar to editing a text file. You type an essay and then discover you incorrectly used "colour" instead of "color" everywhere. Rather than clicking through each word individually, you employ the "Find and Replace" tool in your text editor. The reasoning within computer programming is no different. You are carrying out that replacement process as an automation.
Why It Matters
Replacement of characters is one of the most basic principles of string manipulation. It reinforces your knowledge of how strings are stored and processed. In C, strings are character arrays, and thus you learn how to iterate over and modify arrays. In Java and Python, as strings are immutable, you do not directly modify them, so replace() returns a new string with modifications. This brings forth the aspect of immutability in programming.
Learning Insights
Solving this problem helps beginners learn iteration, conditional checking, and handling strings. They also understand why languages like C permit direct manipulation but higher-level languages make you create new copies. This helps them learn memory management, mutability, and immutability.
Real-World Application
Character replacement is very normal in data cleaning operations, particularly when dealing with text files, logs, or user inputs. For instance, during natural language processing (NLP), you could replace punctuation with spaces. While in cybersecurity, you could replace sensitive characters with masked characters (*). In databases, you can clean the data by replacing unwanted characters prior to saving.
Interview Relevance
This is an interview favorite because it looks easy but will challenge your awareness of string mutability, iteration, and input handling. An interviewer might ask you to do replacement without calling built-in functions, which is a test of your problem-solving ability. For instance, coding your own replace() function from scratch with loops and conditionals is a good show of programming fundamentals.
SEO Optimized Closing
Knowing the way to replace characters in a string in C, C++, Java, and Python is a crucial step for string manipulation beginners. It not only lays a good ground for text processing but also gets you ready for practical uses such as data cleaning, search-and-replace operations, and programming software tasks. By solving this problem, students and interview contestants get to understand how strings function in varying programming languages, the significance of immutability in Java and Python, and effective methods of character manipulation. Understanding replace character logic is useful in coding interviews, competitive programming, and general coding tasks, making it one of the most beneficial and commonly used string programs in programming.
Social Plugin