Open In App

Modifying character in String

Last Updated : 07 Oct, 2023
Comments
Improve
Suggest changes
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();

Output
Geeksforgeeks

Time Complexity: O(1)
Auxiliary Space: O(1)


Next Article
Article Tags :
Practice Tags :

Similar Reads