Function modify a letter in a string Learn programming Java

Lesson:

Functions


Exercise:

Function modify a letter in a string 114


Objetive:

Create a 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");


Code:

public class Main
{
    public static void ChangeChar(String text, int position, char letter)
	{
		text = tangible.StringHelper.remove(text, position, 1);
		text = text.insert(position, String.valueOf(letter));
	}

	public static void main(String[] args)
	{
		String sentence = "Tomato";
		ChangeChar(sentence, 5, 'a');
	}
}