# (Java) Replacing every other vowel with 8



## Senryi (May 27, 2005)

Hey all. Been working on a program that would mimic some typing quirks, and one of them it replacing every other vowel in a sentence with 8.

Here is what I have so far:

*Note: "str" is a non empty string that is passed into the method. And "whole" is a locally defined string.


```
[SIZE="4"]
                        whole = str;
                        for(int i = 0; i < str.length(); i++){
				if(str.charAt(i) == 'a'){
					if(i % 2 == 0){
						whole = whole.replace(str.charAt(i), '8');
					}
					else{
						
					}
				}
				if( str.charAt(i) == 'e'){
					if(i % 2 == 0){
						whole = whole.replace(str.charAt(i), '8');
					}
					else{
						
					}
				}
				if(str.charAt(i) == 'u'){
					if(i % 2 == 0){
						whole = whole.replace(str.charAt(i), '8');
					}
					else{
						
					}
				}
				if(str.charAt(i) == 'i'){
					if(i % 2 == 0){
						whole = whole.replace(str.charAt(i), '8');
					}
					else{
						
					}
				}
				if(str.charAt(i) == 'o'){
					if(i % 2 == 0){
						whole = whole.replace(str.charAt(i), '8');
					}
					else{
						
					}
				}
				else{
					
				}
			}
                        return whole;[/SIZE]
```
Any one got a clue?


----------



## sepala (May 20, 2010)

Remove the Modulo operator. I don't see any use of it in here. Here is the edited code


```
public String userReturn(String str)
    {
        String whole=null;
                                whole = str;
                                System.out.println("Length: "+whole.length());
                        for(int i = 0; i < str.length(); i++){
                if(str.charAt(i) == 'a')
                                {
                        whole = whole.replace(str.charAt(i), '8');
                                }
                    
                    
                
                if( str.charAt(i) == 'e'){
                    
                        whole = whole.replace(str.charAt(i), '8');
                    
                }
                if(str.charAt(i) == 'u'){
                    
                        whole = whole.replace(str.charAt(i), '8');
                    
                }
                if(str.charAt(i) == 'i'){
                        whole = whole.replace(str.charAt(i), '8');
                    
                }
                if(str.charAt(i) == 'o'){
                    
                        whole = whole.replace(str.charAt(i), '8');
                    
                }
                else{
                    
                }
            }
                        System.out.println(whole);
                        return whole; 
                        
    }
```


----------



## Ent (Apr 11, 2009)

sepala said:


> Remove the Modulo operator. I don't see any use of it in here. Here is the edited code


The task is to replace every other vowel with an 8. So the first one should stay as a vowel, the second be 8, the third be a vowel, the fourth an 8, and so on. 
It's not a pointless line.
It's n8t a p8intl8ss lin8.

However, the modulo doesn't do what's expected because it checks whether it's on an even letter, not an even vowel. I don't think there's a quick mathematical function that can do that. What I would suggest is creating a Boolean variable and flipping it every time a vowel is encountered. If it is true, make the vowel an 8. If it is false, leave the vowel as it is. You'd have to do that once you check whether it's a vowel.


----------



## sepala (May 20, 2010)

Ent said:


> The task is to replace every other vowel with an 8. So the first one should stay as a vowel, the second be 8, the third be a vowel, the fourth an 8, and so on.
> It's not a pointless line.
> It's n8t a p8intl8ss lin8.
> 
> However, the modulo doesn't do what's expected because it checks whether it's on an even letter, not an even vowel. I don't think there's a quick mathematical function that can do that. What I would suggest is creating a Boolean variable and flipping it every time a vowel is encountered. If it is true, make the vowel an 8. If it is false, leave the vowel as it is. You'd have to do that once you check whether it's a vowel.


Ahhh, Thanks for clearing it Ent. Again, English was the issue 

Anyway, according to my experience, this is what I do. We have to have a "counter" inside the for loop. If the counter is one, so it will count how many number of vowels are there. If the counter is one, we have to leave it as it is. I will post the code again, but not now because there is just 40 minutes for the New Year 

Happy New Year To All :up:


----------



## sepala (May 20, 2010)

Here is the code


```
public String userReturn(String str)
    {
        String whole=null;
        int a = 0;
                                whole = str;
                                System.out.println("Length: "+whole.length());
                        for(int i = 0; i < str.length(); i++){
                            
                if(str.charAt(i) == 'a' || str.charAt(i)=='e' || str.charAt(i)=='u' || str.charAt(i)=='i' || str.charAt(i)=='o')
                                {
                                    a++;
                                    
                                    if(a==1)
                                    {
                                        
                                    }
                                    else
                                    {
                                    
                        whole = whole.replace(str.charAt(i), '8');
                                    }
                                }
                    
            }
                        System.out.println(whole);
                        return whole; 
                        
    }
```


----------



## Ent (Apr 11, 2009)

Sepala, that won't work. It will miss the first vowel, change the second, and miss all subsequent vowels. 
I believe that this should work, though not being a Java programmer I'm not certain.


```
public String userReturn(String str)
    {
        String whole=null;
        boolean a=true;
                                whole = str;
                                System.out.println("Length: "+whole.length());
                        for(int i = 0; i < str.length(); i++){
                            
                if(str.charAt(i) == 'a' || str.charAt(i)=='e' || str.charAt(i)=='u' || str.charAt(i)=='i' || str.charAt(i)=='o')
                                {
                                    a=!a;
                                    
                                    if(a)
                                    {
                                        whole = whole.replace(str.charAt(i), '8');
                                    }
                                }
                    
            }
                        System.out.println(whole);
                        return whole; 
    }
```
Incidentally, again from a language independent perspective, I'd see a few other problems arising from that code. The first and simplest is that it misses capital vowels. I was also just writing a post about how you could put the five checks into one with a simple OR operator, and then you built it straight into your code.


----------



## Senryi (May 27, 2005)

This is just one piece of the method to mimic the typing quirk. In this same quirk, if a vowel is capitalized it gets replace regardless if it is every other one. I already have the code for that, it is just this one bit that is confusing me. Thanks for the help guys. I will get back to ya when I get it working.


----------



## sepala (May 20, 2010)

Ent said:


> Sepala, that won't work. It will miss the first vowel, change the second, and miss all subsequent vowels.
> I believe that this should work, though not being a Java programmer I'm not certain.
> 
> 
> ...


Thanks for showing that mistake Ent. I wasn't reading it GOOD I guess. Anyway the given code of yours is not working for all the places. The following is also will give the output, but not for all, it has the same issues as in you code.


```
public class User
{
    
    public String userReturn(String str)
    {
        String whole=null;
        int a = 0;
        int mod = 0;
                                whole = str;
                                System.out.println("Length: "+whole.length());
                        for(int i = 0; i < str.length(); i++){
                            
                if(str.charAt(i) == 'a' || str.charAt(i)=='e' || str.charAt(i)=='u' || str.charAt(i)=='i' || str.charAt(i)=='o')
                                {
                                    a++;
                                    
                                    if(a%2 != 0 || a==1)
                                    {
                                        
                                    }
                                    
                                    else
                                    {
                                    
                        whole = whole.replace(str.charAt(i), '8');
                                    }
                                }
                                
                    
            }
                        System.out.println(whole);
                        return whole; 
                        
    }
    
    public static void main(String[]args)
    {
        User u = new User();
        u.userReturn("tech support guy forums");
        u.userReturn("sri lanka");
    }
}
```
output



> Length: 23
> tech s8pport g8y for8ms
> Length: 9
> sri l8nk8


The best thing is, I found the error. Both the examples, written by you and me, and MESSING with 'a' . Whenever the 'a' comes, it is a mess.


----------



## sepala (May 20, 2010)

Anyway, the following code is working without any issues


```
public class User
{
    
    public String userReturn(String str)
    {
        char whole[]=null;
        int a = 0;
        int mod = 0;
        String local = str.toLowerCase();
        
                                whole = local.toCharArray();
                                
                        for(int i = 0; i < whole.length;i++)
                        {
                            
                if(whole[i]=='a' || whole[i]=='e' || whole[i]=='u' || whole[i]=='i' || whole[i]=='o')
                                {
                                    a++;
                                    
                                    if(a%2 != 0 || a==1)
                                    {
                                        
                                    }
                                    
                                    else
                                    {
                                    
                        whole[i] = '8';
                                    }
                                }
                                
                    
            }
                        System.out.println(whole);
                        return whole.toString(); 
                        
    }
    
    public static void main(String[]args)
    {
        User u = new User();
        u.userReturn("tEch support guy forums");
        u.userReturn("sri lanka");
        u.userReturn("seems like this is working");
    }
}
```
output



> tech s8pport g8y for8ms
> sri l8nka
> se8ms lik8 this 8s work8ng


----------



## Ent (Apr 11, 2009)

No surprises that they share the same problems, I copied your code as a base. 
The mod code you've used works, but it would be better to use the boolean technique. Basically flipping a single bit is about the simplest operation available, more so than adding to an integer. It's also already a true/false value for the If statement, so you don't need the extra calculations to get the mod. 

The advantage of keeping the count as in your method would be if you also needed to know how many vowels there were.


----------



## sepala (May 20, 2010)

TO the OP.

In cases like these, it is always better to convert the whole into the NEEDED format first. It will be easy and less troubled. I had that idea in my mind at the first time, but I didn't expose it because I thought you need your own code to be SLIGHTLY edited.


----------

