Results 1 to 3 of 3
  1. #1
    Renxx is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Replacing all non letters with symbol * in string

    My program only replaces the last non letter in string. Could someone help me:)?
    Java Code:
    Write a Java method to replace all non-letters in a given string s by symbol '*'. 
    public static String asenda (String s)
    Java Code:
    package soned;
    
    
    public class soneExample3 {
    
       /**
        * @param args
        */
       public static void main(String[] args) {
       System.out.println(asenda(new String("H'o.m-m i_k")));
       }
    
       public static String asenda(String s) {
          String result ="";
          
          for(int i=0;i<s.length();i++) {
             if(!Character.isLetter(s.charAt(i))) {
               result = s.replace(s.charAt(i), '*');
             }
          }
          
          return result;  
       }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Replacing all non letters with symbol * in string

    You're halfway there: you have to iterate over all characters in the String; if the current character is a letter you simply append it to the result; if it isn't a letter you append a '*' character to the result instead. Something like this:

    Java Code:
    char current= s.charAt(i);
    if (Character.isLetter(current)) result+= current;
    else result+= '*';
    The code snippet above should be embedded in the for-loop body of your code.

    kind regards,

    Jos

    ps. I removed your duplicate thread.
    Last edited by JosAH; 11-22-2011 at 08:55 PM.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Renxx is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Replacing all non letters with symbol * in string

    Thank You :)

Similar Threads

  1. Shifting String Letters
    By DreamNaut in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 08:12 PM
  2. counting letters in a string
    By beandip408 in forum New To Java
    Replies: 12
    Last Post: 09-29-2010, 01:44 PM
  3. increment letters from a string
    By iluvjava in forum New To Java
    Replies: 24
    Last Post: 03-22-2010, 05:18 PM
  4. Help Replacing String
    By 7oclock in forum New To Java
    Replies: 5
    Last Post: 02-14-2009, 07:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •