• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to replace every second character in a sequence

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Sheriff
Posts: 17711
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 doc:  http://docs.oracle.com/javase/8/docs/api/index.html
 
Marshal
Posts: 79977
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic