Function Modify A Letter In A String - C# Programming Exercise

In this exercise of C#, you will learn how to create a function named ChangeChar that modifies a letter at a specific position (starting from 0) in a string. The function replaces the character at the specified position with a different letter. This type of exercise is essential for understanding how to manipulate strings in C# and how to pass variables by reference using the ref modifier. In the provided example, the original string is "Tomato" and the function replaces the character at position 5 with the letter "a". These types of operations are common when working with text in programming, so mastering this technique will allow you to make dynamic modifications to text strings.

Learn how to make precise and efficient modifications to character strings with this hands-on exercise and improve your skills in C# with string manipulation and the use of reference parameters.

 Category

Functions

 Exercise

Function Modify A Letter In A String

 Objective

Write a C# function named "ChangeChar" to modify a letter in a certain position (0 based) of a string, replacing it with a different letter:

string sentence = "Tomato";
ChangeChar(ref sentence, 5, "a");

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Function to modify a character in a string at a given position
    public static void ChangeChar(ref string str, int position, string newChar)
    {
        // Convert the string into a character array (since strings are immutable in C#)
        char[] charArray = str.ToCharArray();

        // Replace the character at the specified position with the new character
        charArray[position] = newChar[0]; // newChar is assumed to be a single character string

        // Convert the character array back to a string and update the original string
        str = new string(charArray);
    }

    // Main method where the ChangeChar function is called
    public static void Main()
    {
        // Original string
        string sentence = "Tomato";

        // Call the ChangeChar function to change the character at position 5 to 'a'
        ChangeChar(ref sentence, 5, "a");

        // Print the modified string
        Console.WriteLine("Modified string: " + sentence);
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function IsPrimeTarea

    In this exercise of C#, you will learn how to create a function named IsPrime that receives an integer number and returns true if the number is p...

  •  Function Parameters of Main, Sum

    In this exercise of C#, you will learn how to create a program named sum that receives two integer numbers from the command line and displays their sum. This t...

  •  Function SumDigits

    In this exercise of C#, you will learn how to create a function named SumDigits that receives a number and returns the result of summing its digits. Thi...

  •  Function Factorial

    In this exercise of C#, you will learn how to create a recursive function to calculate the factorial of a number. The factorial of a number is ex...

  •  Function Parameters of Main, Reverse

    In this exercise of C#, you will learn how to create a program named reverse that receives several words from the command line and display...

  •  Function GetInt

    In this exercise of C#, you will learn how to create a function named GetInt, which displays on screen the text received as a parameter, asks the user f...