7 Replies - 1038 Views - Last Post: 03 March 2016 - 06:56 AM Rate Topic: -----

#1 karengsh  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 129
  • Joined: 18-May 09

How do I replace the vowel in a phrase with another character?

Posted 02 March 2016 - 02:38 AM

Hi,

I am facing problem with replacing the vowel in a phrase with another character say *.

This is what I have attempted so far and I am hoping someone can help me correct my code to achieve the result.

We can't use array; can only use Characters and String, StringBuilder class to do it.




public class WordPlay {

	
		
	public static String replaceVowel(String phrase, char ch){
		StringBuilder input = new StringBuilder(phrase);
		String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		int k = 0;
		
		for (int i = 0; i < phrase.length(); i++){
			char to = '*';
			char currch = phrase.charAt(i);
			if (currch ==  'a' || currch == 'e' || currch == 'i' || currch == 'o' || currch == 'u' ||
				currch == 'A' || currch == 'E' || currch == 'I' || currch == 'O' || currch == 'U' ){ 
					
				int idx = alphabet.indexOf(currch);
				currch += to;
				//String newChar = alphabet.replace(currch, to);
				//System.out.println("new phrase" + currch);
				input.setCharAt(idx, to);
			}
			// otherwise: do nothing
		}				
			return input.toString();
		}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WordPlay wp = new WordPlay();
		
		String resultReplace = replaceVowel("Hello World", '*' );
		
		System.out.println(resultReplace);
	}

}








Is This A Good Question/Topic? 0
  • +

Replies To: How do I replace the vowel in a phrase with another character?

#2 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6341
  • View blog
  • Posts: 25,584
  • Joined: 12-December 12

Re: How do I replace the vowel in a phrase with another character?

Posted 02 March 2016 - 03:09 AM

Please post questions in the Java forum, not the Discussion Lounge. Topic moved.
Was This Post Helpful? 0
  • +
  • -

#3 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: How do I replace the vowel in a phrase with another character?

Posted 02 March 2016 - 04:20 AM

Quote

help me correct my code to achieve the result.

Can you post the console from when the code is executed? It should show the input to the method and the results.

Add some comments saying what is wrong and show what the results should be.

Also some comments in the code saying what the statements are doing and why they are doing it would be helpful. For example add comments to explain why these statements are in the code:
	int idx = alphabet.indexOf(currch);
	currch += to;


This post has been edited by NormR: 02 March 2016 - 04:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3425
  • View blog
  • Posts: 15,501
  • Joined: 20-September 08

Re: How do I replace the vowel in a phrase with another character?

Posted 02 March 2016 - 08:04 AM

If you're allowed to use the methods of String, you could of course do it in one line with String.replaceAll
Was This Post Helpful? 0
  • +
  • -

#5 karengsh  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 129
  • Joined: 18-May 09

Re: How do I replace the vowel in a phrase with another character?

Posted 02 March 2016 - 08:37 PM

Hi,

I managed to do it.

public static String replaceVowel(String phrase, char ch){
		StringBuilder input = new StringBuilder(phrase);
	
		
		for (int i = 0; i < input.length(); i++){
			char to = '*';
		 	char currch = input.charAt(i);
			if (currch ==  'a' || currch == 'e' || currch == 'i' || currch == 'o' || currch == 'u' ||
				currch == 'A' || currch == 'E' || currch == 'I' || currch == 'O' || currch == 'U' ){ 					
				
				input.setCharAt(i, to);				
						
			}
			// otherwise: do nothing
		}				
			return input.toString();
		}



Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3425
  • View blog
  • Posts: 15,501
  • Joined: 20-September 08

Re: How do I replace the vowel in a phrase with another character?

Posted 03 March 2016 - 01:32 AM

What is ch for and what is it doing?
Was This Post Helpful? 0
  • +
  • -

#7 karengsh  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 129
  • Joined: 18-May 09

Re: How do I replace the vowel in a phrase with another character?

Posted 03 March 2016 - 06:28 AM

View Postg00se, on 03 March 2016 - 01:32 AM, said:

What is ch for and what is it doing?


Hi,

Tks for pointing out but I think I should change char to to char ch.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3425
  • View blog
  • Posts: 15,501
  • Joined: 20-September 08

Re: How do I replace the vowel in a phrase with another character?

Posted 03 March 2016 - 06:56 AM

Correct!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1