Modifying character in String

To modify any Character in a String, we need:

  1. A character that is to replaced in the string (say “ch”)
  2. A position/index of the Character where it is to be replaced at. (say “k”)

Below is the implementation of the above approach:

C++
#include <iostream>
#include <string>

int main()
{
    // Get the string
    std::string str = "Geeks Gor Geeks";

    // Get the index
    int index = 6;

    // Get the character
    char ch = 'F';

    // Print the original string
    std::cout << "Original String = " << str << std::endl;

    str.replace(index, 1, 1, ch);

    // Print the modified string
    std::cout << "Modified String = " << str << std::endl;

    return 0;
}
C
#include <stdio.h>
#include <string.h>

int main()
{
    // Define the string
    char str[] = "Geeks Gor Geeks";

    // Define the index
    int index = 6;

    // Define the character
    char ch = 'F';

    // Print the original string
    printf("Original String = %s\n", str);

    // Modify the string
    str[index] = ch;

    // Print the modified string
    printf("Modified String = %s\n", str);

    return 0;
}
Java
public class GFG {

    public static void main(String args[])
    {

        // Get the String
        String str = "Geeks Gor Geeks";

        // Get the index
        int index = 6;

        // Get the character
        char ch = 'F';

        // Print the original string
        System.out.println("Original String = " + str);

        str = str.substring(0, index) + ch
            + str.substring(index + 1);

        // Print the modified string
        System.out.println("Modified String = " + str);
    }
}
Python
# Function to replace a character at a specific index in a string
def replace_character_at_index(string, index, new_character):

    # Convert string to list of characters to allow modification
    string_list = list(string)

    # Replace character at the specified index
    string_list[index] = new_character

    # Convert back to string and return
    return ''.join(string_list)

# Main function


def main():
    # Original string
    original_string = "Geeks Gor Geeks"

    # Index to replace character
    index = 6

    # New character
    new_character = 'F'

    # Print original string
    print("Original String =", original_string)

    # Replace character at the specified index
    modified_string = replace_character_at_index(
        original_string, index, new_character)

    # Print modified string
    print("Modified String =", modified_string)


# Entry point of the program
if __name__ == "__main__":
    main()
C#
using System;

public class GFG
{
    public static void Main()
    {
        // Get the String
        string str = "Geeks Gor Geeks";

        // Get the index
        int index = 6;

        // Get the character
        char ch = 'F';

        // Print the original string
        Console.WriteLine("Original String = " + str);

        // Modify the string
        str = str.Substring(0, index) + ch + str.Substring(index + 1);

        // Print the modified string
        Console.WriteLine("Modified String = " + str);
    }
}
JavaScript
let str = "Geeks Gor Geeks"; // Get the string
let index = 6; // Get the index
let ch = 'F'; // Get the character

console.log("Original String = " + str); // Print the original string

// Modify the string
str = str.substr(0, index) + ch + str.substr(index + 1);

console.log("Modified String = " + str); // Print the modified string

Output
Original String = Geeks Gor Geeks
Modified String = Geeks For Geeks

Basic String Operations with Implementation

In this post, we will look into some of the basic String operations such as:

  • Accessing characters by index in a string.
  • Inserting character into a String.
  • Modifying character in String
  • Deletion of Character in String
  • Concatenating strings (combining multiple strings into one).
  • Finding the length of a string
  • Comparing strings for equality or lexicographical order

Let us consider the basic String operations one by one.

Similar Reads

Accessing characters by index in a string.

To access any character in a String, we need:...

Inserting Character/String into an String.

To insert any Character/String in a String, we need:...

Modifying character in String

To modify any Character in a String, we need:...

Deletion of character in String

To delete any Character in a String, we need:...

Concatenating strings (combining multiple strings into one).

To concatenate any String to a String, we need:...

Finding the length/size of a string

To find the length of the String, we need:...

Comparing Strings for Equality

To compare strings, Define a function to compare values with the following conditions :...

Contact Us