The Java string replace() method will replace a character or substring with another character or string. Great answer. I have created following extension methods to perform this operation: If the data is tokenized (i.e. I refactored it as an extension method for Regex, which I can't show in a comment so will add as a somewhat redundant extra answer below. scanf("%d",&a); The complete program is listed in Listing 1. How do I iterate over the words of a string? Method calls are executed from left to right. We can easily replace a character in it using the indexing operator. You can use a different variable #include