Modifying character in String Last Updated : 07 Oct, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string str, an integer index, and a character ch, the task is to modify the character at index in a given string and replace it with character ch. Examples: Input: str = "geeksforgeeks", index = 0, ch = 'G'Output: Geeksforgeeks Input: str = "spacing", index = 2, ch = '*'Output: sp*cing Approach: To solve the problem follow the below idea: Assign the character 'ch' at index given in the problem. Below is the implementation of the above approach: C++ // C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to modifying character in String void modifyString(string& s, char ch, int index) { if (index < s.length()) s[index] = ch; } // Driver code int main() { string s = "geeksforgeeks"; int index = 0; char ch = 'G'; // Modifying our string modifyString(s, ch, index); // Printing the resultant string cout << s << endl; } Java /*package whatever //do not write package name here */ import java.io.*; public class Main { // Function to modifying // character in String public static void modifyString(StringBuilder s, char ch, int index) { if (index < s.length()) s.setCharAt(index, ch); } // Driver code public static void main(String[] args) { StringBuilder s = new StringBuilder("geeksforgeeks"); int index = 0; char ch = 'G'; // modifying our string modifyString(s, ch, index); // Printing the resultant string System.out.println(s); } } Python3 # Function to modifying #character in String def modify_string(s, ch, index): if index < len(s): s[index] = ch # Driver code if __name__ == "__main__": s = list("geeksforgeeks") index = 0 ch = 'G' # Modifying our string modify_string(s, ch, index) # Printing the resultant string print(''.join(s)) C# // C# code to implement the approach using System; class Program { // Function to modifying character in String static void ModifyString(ref string s, char ch, int index) { if (index < s.Length) s = s.Substring(0, index) + ch + s.Substring(index + 1); } // Driver code static void Main(string[] args) { string s = "geeksforgeeks"; int index = 0; char ch = 'G'; // Modifying our string ModifyString(ref s, ch, index); // Printing the resultant string Console.WriteLine(s); } } JavaScript function GFG(s, ch, index) { // Check if the index is within the string's length if (index < s.length) { // Convert the string to an array to // modify the character const stringArray = s.split(''); // Modify the character at the specified index stringArray[index] = ch; // Join the array back into a string s = stringArray.join(''); } return s; } // Driver code function main() { let s = "geeksforgeeks"; let index = 0; let ch = 'G'; s = GFG(s, ch, index); // Printing the resultant string console.log(s); } main(); OutputGeeksforgeeksTime Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Remove Special Characters from a String in Ruby? S srinam Follow Improve Article Tags : Strings DSA Practice Tags : Strings Similar Reads How to modify a string in JavaScript ? JavaScript strings are used for storing and manipulating text. It can contain zero or more characters within quotes and its indexing starts with 0. Strings are defined as an array of characters. In Javascript, we can also convert strings to a Character array. Representing a String: 1. Using double 3 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read How to Remove Special Characters from a String in Ruby? In this article, we will discuss how to remove special characters from a string in Ruby. In Ruby, special characters can be removed from a string using various methods. Special characters typically include punctuation marks, symbols, and non-alphanumeric characters. Let us explore different methods 2 min read Bash Scripting - String Bash String is a data type similar to integer or boolean. It is generally used to represent text. It is a string of characters that may also contain numbers enclosed within double or single quotes. Example: "geeksforgeeks", "Geeks for Geeks" or "23690" are strings Creating a String A basic declarati 2 min read Strings in Objective-C Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the 4 min read How to get string between two characters in PHP ? A string is a sequence of characters stored in the incremental form in PHP. A set of characters, one or many can lie between any two chosen indexes of the string. The text between two characters in PHP can be extracted using the following two methods : Table of ContentUsing substr() Method: Using fo 4 min read Like