• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String and for Loop Confusion

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good evening all,

I've been staring at the below problem and am more confused than before hand. I've done strings before and replaced letters within them but never in a for loop. I've only used the replace method which is something that we cannot use. Any guidance would be greatly appreciated.

Let word be a String variable referencing a non-empty string. Write a for loop that prints the string with all occurrences of the lowercase letter 'n' replaced with 'r'. For example, if word references the string "Kenny", then your code should print "Kerry". Do not use the replace() method of the String class.

When I've done for loops in the past it was for integers and when I try and code for chars & strings I get lost.
 
Ranch Hand
Posts: 349
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Are you aware of replaceAll() method in Strings?

Ananth Chellathurai
 
Mike B.
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No I haven't heard of it at all. Does it have the look and feel of the replace() method? If so then I'm hamstrung from using it.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets think this through. Using a for loop, you'll want to examine each character of the String from the first to the last. If the current character is not an 'n', then print it, else print an 'r'. Look at the methods of String class. Which String methods do you think might be useful to you?
[ October 15, 2008: Message edited by: Garrett Rowe ]
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Mike B." please read the important administrative private message I have just sent you.

CR
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic