Recursion

Replacing characters in a string

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2457 Views - Last Post: 13 August 2010 - 05:38 AM Rate Topic: -----

#1 thePhantom  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 60
  • Joined: 28-April 10

Recursion

Posted 10 August 2010 - 09:50 AM

hi
I have to replace a certain character(chosen by the user) in a given string, with another character(inputted by user). E.g:
"enter a string:" Good Morning
"inputted by user:" o
"change to:" z
"final output:" Gzzd Mzrning

The problem is that Im not allowed to use loops or the replace method, only basic java stuff...This is my code so far(although it doesnt compile)

import java.util.Scanner;
    public class Recur
   {
       public static void main (String[] args)
      {
         Scanner input=new Scanner(System.in);
        
         String word= input.nextLine();
         int  spaces=word.length(); 
        
         String [] letter= new String [spaces];
         String swop=input.nextLine(); 
         String substitute=input.nextLine();
         
         System.out.println(word);
         System.out.println(swop);
         System.out.println(substitute);
      
       
         int a=0,b=0;  
      
         if(a<letter.length)
         { 
            if(letter[a]==swop) {
               letter[a]=substitute;
               b=1; }
            a++; }
      	
         if(b==1)
         { String revised = new String(letter);
            System.out.println(revised); }
      	
         if(b==0)
         {  System.out.println(""); }   
      	  
      }
   }


Any help will be greatly appreciated as i need to finish this tonight. thnx

Is This A Good Question/Topic? 0
  • +

Replies To: Recursion

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 616
  • Joined: 02-December 09

Re: Recursion

Posted 10 August 2010 - 09:57 AM

I really don't know how to do that with out use any loop.
Was This Post Helpful? -1
  • +
  • -

#3 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 616
  • Joined: 02-December 09

Re: Recursion

Posted 10 August 2010 - 10:07 AM

Ok I figured out how you can do it. First you need ask for the character to replace, then the character to substitute and then you ask for the string to change but reading character by character:
BufferedReader buffer = new BufferedReader(
                 new InputStreamReader(System.in));
        int c = 0;
        while((c = buffer.read()) != 13) {
                char character = (char) c;
   //here change the character you need and asign it to a new String. Use a StringBuffer.
  
        

     }



if the user input ENTER it finish.

This post has been edited by guidojavier: 10 August 2010 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

#4 thePhantom  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 60
  • Joined: 28-April 10

Re: Recursion

Posted 10 August 2010 - 10:11 AM

hey, thnx for your help...but "while" is also a loop, and we haven't learnt about StringBuffer yet, therefore cannot use it :(
Was This Post Helpful? 0
  • +
  • -

#5 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 616
  • Joined: 02-December 09

Re: Recursion

Posted 10 August 2010 - 10:16 AM

sorry you're right I forgot that 'while' was a loop...hummm I suppose that if your teacher gave you that assignment is because it is possible do it.
Was This Post Helpful? 0
  • +
  • -

#6 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Recursion

Posted 10 August 2010 - 10:53 AM

Hey there,

This may be a silly question but since you have the word recursion in the title of the post is it a requirement of the assignment to use recursion? I'm only asking because you don't mention it in the post body.

View PostthePhantom, on 10 August 2010 - 08:50 AM, said:

hi
I have to replace a certain character(chosen by the user) in a given string, with another character(inputted by user). E.g:
"enter a string:" Good Morning
"inputted by user:" o
"change to:" z
"final output:" Gzzd Mzrning

The problem is that Im not allowed to use loops or the replace method, only basic java stuff...This is my code so far(although it doesnt compile)

import java.util.Scanner;
    public class Recur
   {
       public static void main (String[] args)
      {
         Scanner input=new Scanner(System.in);
        
         String word= input.nextLine();
         int  spaces=word.length(); 
        
         String [] letter= new String [spaces];
         String swop=input.nextLine(); 
         String substitute=input.nextLine();
         
         System.out.println(word);
         System.out.println(swop);
         System.out.println(substitute);
      
       
         int a=0,b=0;  
      
         if(a<letter.length)
         { 
            if(letter[a]==swop) {
               letter[a]=substitute;
               b=1; }
            a++; }
      	
         if(b==1)
         { String revised = new String(letter);
            System.out.println(revised); }
      	
         if(b==0)
         {  System.out.println(""); }   
      	  
      }
   }


Any help will be greatly appreciated as i need to finish this tonight. thnx

Was This Post Helpful? 0
  • +
  • -

#7 thePhantom  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 60
  • Joined: 28-April 10

Re: Recursion

Posted 10 August 2010 - 10:56 AM

Yes, its an absolute must that i use recursion and that the words "for" "while" and "replace" do not appear in my program
Was This Post Helpful? 0
  • +
  • -

#8 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Recursion

Posted 10 August 2010 - 11:11 AM

You do know what recursion means, right? Recursion happens when a method calls upon itself when a condition is met, until a base case is met, and thus stopping the method and returning a value. I don't even see any methods used in your code...
Was This Post Helpful? 1
  • +
  • -

#9 thePhantom  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 60
  • Joined: 28-April 10

Re: Recursion

Posted 10 August 2010 - 11:18 AM

err, sori...Im still new to this java and recursion stuff...I am reading up on the textbook, also started to go through a few examples...
hopefully Im getting sumwhere...slowly, but surely
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Recursion

Posted 10 August 2010 - 11:44 AM

here's one way to do it
public static String replace(String str, char old, char newChar, int index)
{
    if(str.length() == 0 || index == str.length())
        return "";
    if(old == str.charAt(index))
        return newChar+replace(str, old, newChar, ++index);
    return str.charAt(index)+replace(str, old, newChar, ++index);
}



of course there are better ways to do this, try to think about them ;)
Was This Post Helpful? 3
  • +
  • -

#11 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Recursion

Posted 10 August 2010 - 02:26 PM

Can always do it like this, which rids your index parameter...seems a bit awkward to have that parameter:
  public static String replaceCharacters(String line, char change, char newChar)
  {
    if(line.indexOf(change) == -1)
      return line;
    char[] lineArray = line.toCharArray();
    lineArray[line.indexOf(change)] = newChar;
    return replaceCharacters(new String(lineArray),change,newChar);
  }


Was This Post Helpful? 1
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Recursion

Posted 10 August 2010 - 03:49 PM

having index makes it more flexible actually, you can choose from where to begin replacing the characters
Was This Post Helpful? 0
  • +
  • -

#13 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Recursion

Posted 10 August 2010 - 03:57 PM

View Postmostyfriedman, on 10 August 2010 - 02:49 PM, said:

having index makes it more flexible actually, you can choose from where to begin replacing the characters

The OP wants ALL to be replaced :)
Was This Post Helpful? 0
  • +
  • -

#14 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Recursion

Posted 10 August 2010 - 03:58 PM

yea i know, i am just saying :P.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Recursion

Posted 10 August 2010 - 04:46 PM

View PostthePhantom, on 10 August 2010 - 10:50 AM, said:

The problem is that Im not allowed to use loops or the replace method, only basic java stuff

And you consider recursion as being a basic stuff ?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2