Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to replace every second character in a sequence  RSS feed

 
Antonio Gridushko
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everyone!

I need to understand which class and method in Java I should use to replace every, for example, 3d letter  in any sentence.

For example,  in this sentence: "This period was good" I want to replace every 3 d letter... with "r" exactly.... so at the end of compilation I should have


"Thrs perior war gord"

And if the letter in this sentense consists from only one letter- there is no changing.


 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

What have you tried so far? Please show us your code.

Also, see the links in my signature below for tips on how to ask questions in these forums.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Antonio,welcome to ranch!
do you have tried to write a pseudo code,if not please take a piece of paper and write it.

Hint:-you can use "split"and "toCharArray" method of String class.
----->API link for more information.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which class and method 

Since the sentence is contained in a String, start by looking at the String class.  Also the StringBuilder and StringBuffer classes would be useful.
See the API dochttp://docs.oracle.com/javase/8/docs/api/index.html
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
Did you really say to replace every 3rd letter in a sequence, or did you mean in a String? Did you know there is a class specifically designed for altering Strings? It even has a method for replacing characters. That would be another possible solution.
 
Antonio Gridushko
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again
Did you really say to replace every 3rd letter in a sequence, or did you mean in a String? Did you know there is a class specifically designed for altering Strings? It even has a method for replacing characters. That would be another possible solution.


Thanks everyone, I found my answer


 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Antonio,Good job!
please use code tag,it will make your code look better and readable to others.their are lot of features on ranch for posting,so utilize them.

Thanks,
kind regards,
Praveen.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sample results: "Thrs perior war gord" showed some replacements but I'm not sure what rules were used.
For me replacing every third letter in: 
would replace the letters above the 3s shown above.  If the spaces are not considered letters.
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run your code it doesn't replace every 2nd character.

Enter text string
aaaaaaaaa bbbbbbb
Enter number
2
Enter symbol
x
aaxaaaaaa
bbxbbbb
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!