Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.util.Scanner;
import java.util.Stack;
public class a 
{
public static String
	remove(String str)
	{
	  str = str.replaceAll("[^a-zA-Z0-9]", "");
	 
	  return str;
	}
	
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		
		System.out.print("Enter a String: ");
		String str = sc.nextLine();
		
		System.out.println(remove(str));
		
		Stack<Character> vowel = new Stack<>();
		Stack<Character> consonant = new Stack<>();
				
		for(int i = 0; i < str.length(); i++)
			
			if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || 
                str.charAt(i) == 'i' || str.charAt(i) == 'o' || 
                str.charAt(i) == 'u')
			{
				vowel.push(str.charAt(i));
			}
			else
			{
				consonant.push(str.charAt(i));
			}
				System.out.print("vowels: " + vowel);
				System.out.print("\n" + "consonant: " + consonant);
	}
}


What I have tried:

So my output is this:

Enter a String: 1234hello!@#$
1234hello
vowels: [e, o]
consonant: [1, 2, 3, 4, h, l, l, !, @, #, $]

how can i pop or delete the non alphabet characters on the consonant part?

i tried putting a remove part but it only removes the non stacked String. I want to remove the non alphabet characters on the stack.
Posted
Updated 17-Sep-21 22:07pm

1 solution

You need to capture the returned string from the call to remove:
Java
str = remove(str);
System.out.println(str);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900