What's new
  • Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

Pa help po sa String problem nato


derp123412

Grasshopper
Joined
Jan 18, 2016
Messages
34
Reaction
9
Java program using String function/s that will convert vowels to asterisks. Your program should ask for a user input.


SAMPLE OUTPUT:

Enter a string: Happy New Year

New string: H*ppy N*w Y**r
 

Arcturus

Forum Guru
, Last edited:
hmm, pa try nito paps
using java.lang.StringBuilder
Java:
import java.util.Scanner;
public class test2 {
    static boolean isVowel(char c) {
        switch(c) {
        case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U':
            return true;
        }
        return false;
    }
    static Scanner sc = new Scanner (System.in);
    public static void main(String []args){
        StringBuilder sb = new StringBuilder();
        System.out.println("Input: ");
        String input = sc.nextLine();
      
        for(int i = 0 ; i < input.length(); i++) {
            if(isVowel(input.charAt(i)))
                sb.append('*');
            else
            sb.append(input.charAt(i));
              
        }
        System.out.println("Output: "+sb.toString());
    }
}
or this kung hindi StringBuilder ang gagamitin
Java:
import java.util.Scanner;
public class test2 {
    static boolean isVowel(char c) {
        switch(c) {
        case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U':
            return true;
        }
        return false;
    }
    static Scanner sc = new Scanner (System.in);
    public static void main(String []args){
        System.out.println("Input: ");
        String input = sc.nextLine();
        System.out.print("Output: ");
       
        for(int i = 0 ; i < input.length(); i++) {
            if(isVowel(input.charAt(i)))
                System.out.print("*");
            else
            System.out.print(""+input.charAt(i));
               
        }
    }
}
 
Comment

derp123412

Grasshopper
hmm, pa try nito paps
using java.lang.StringBuilder
Java:
import java.util.Scanner;
public class test2 {
    static boolean isVowel(char c) {
        switch(c) {
        case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U':
            return true;
        }
        return false;
    }
    static Scanner sc = new Scanner (System.in);
    public static void main(String []args){
        StringBuilder sb = new StringBuilder();
        System.out.println("Input: ");
        String input = sc.nextLine();
     
        for(int i = 0 ; i < input.length(); i++) {
            if(isVowel(input.charAt(i)))
                sb.append('*');
            else
            sb.append(input.charAt(i));
             
        }
        System.out.println("Output: "+sb.toString());
    }
}
or this kung hindi StringBuilder ang gagamitin
Java:
import java.util.Scanner;
public class test2 {
    static boolean isVowel(char c) {
        switch(c) {
        case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U':
            return true;
        }
        return false;
    }
    static Scanner sc = new Scanner (System.in);
    public static void main(String []args){
        System.out.println("Input: ");
        String input = sc.nextLine();
        System.out.print("Output: ");
      
        for(int i = 0 ; i < input.length(); i++) {
            if(isVowel(input.charAt(i)))
                System.out.print("*");
            else
            System.out.print(""+input.charAt(i));
              
        }
    }
}

Maraming salamat po paps! tama po ang binigay mo na code.
 
Comment

mrHazan

Eternal Poster
Ito sa akin:

Java:
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        
        String string = "";
        char[] stringArray;
        
        System.out.print("Input: ");
        string = scan.nextLine();
        stringArray = string.toCharArray();
        
        for (int i = 0; i < stringArray.length; i++)
        {
            switch(stringArray[i])
            {
                case 'a':
                case 'A':
                case 'e':
                case 'E':
                case 'i':
                case 'I':
                case 'o':
                case 'O':
                case 'u':
                case 'U':
                    stringArray[i] = '*';
                    break;
                default:
            }
        }
        
        string = new String(stringArray);
        System.out.print("\nOutput: " + string);
    }
}
 
Comment

derp123412

Grasshopper
Ito sa akin:

Java:
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
       
        String string = "";
        char[] stringArray;
       
        System.out.print("Input: ");
        string = scan.nextLine();
        stringArray = string.toCharArray();
       
        for (int i = 0; i < stringArray.length; i++)
        {
            switch(stringArray[i])
            {
                case 'a':
                case 'A':
                case 'e':
                case 'E':
                case 'i':
                case 'I':
                case 'o':
                case 'O':
                case 'u':
                case 'U':
                    stringArray[i] = '*';
                    break;
                default:
            }
        }
       
        string = new String(stringArray);
        System.out.print("\nOutput: " + string);
    }
}
parang ito gusto ipa gawa sa ami gamit loop at switch. maraming salamat po paps!
 
Comment

mrHazan

Eternal Poster
parang ito gusto ipa gawa sa ami gamit loop at switch. maraming salamat po paps!
Sa totoo lang may shorter version pa yan. GAgamit ka ng replaceAll. no loop, no switch.


Java:
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        String string = "";
     
        System.out.print("Input: ");
        string = scan.nextLine();
        System.out.print("\nOutput: " + string.replaceAll("AEIOUaeiou", "*"));
    }
}
 
Comment

derp123412

Grasshopper
Sa totoo lang may shorter version pa yan. GAgamit ka ng replaceAll. no loop, no switch.


Java:
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        String string = "";
    
        System.out.print("Input: ");
        string = scan.nextLine();
        System.out.print("\nOutput: " + string.replaceAll("AEIOUaeiou", "*"));
    }
}
aahhhhh . . . . . . . . . . .
 
Comment

twojuantwo

Leecher
, Last edited:
Code:
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Input: ");
        String myString = scan.nextLine().replaceAll("[AEIOUaeiou]", "*");
        System.out.format("Output: %s: ", myString);
    }
 
Comment

pixkit

Addict
parang ito gusto ipa gawa sa ami gamit loop at switch. maraming salamat po paps!
I hope your professor is asking you this requirement to train his students on loops and switch and NOT because he thinks this is the acceptable way to solve this problem because it's not.
 
Comment

derp123412

Grasshopper
I hope your professor is asking you this requirement to train his students on loops and switch and NOT because he thinks this is the acceptable way to solve this problem because it's not.
she told us to find an answer and if we don't find an answer she will explain it to us. I was just asking becuase i'm still new at using strings but i know the basics for loops and switch. thanks for your concern though :)
 
Comment

fireclouu

Addict
, Last edited:
pasali ako :)
Java:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter String: ");
String str = scanner.nextLine();

        // Prepare new line
        System.out.println();

        // Prepare container (concatenation method)
        String outStr = "";

        for(int i = 0; i < str.length(); i++) {
            // uses implicit casting
            boolean isMatched = Pattern.matches("[AEIOUaeiou]", "" + str.charAt(i));

            if(isMatched) {
                outStr += "*";
            } else {
                outStr += str.charAt(i);
            }
        }

        System.out.println(outStr);
 
Comment

aa1012

Addict
pasali ako :)
Java:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter String: ");
String str = scanner.nextLine();

        // Prepare new line
        System.out.println();

        // Prepare container (concatenation method)
        String outStr = "";

        for(int i = 0; i < str.length(); i++) {
            // uses implicit casting
            boolean isMatched = Pattern.matches("[AEIOUaeiou]", "" + str.charAt(i));

            if(isMatched) {
                outStr += "*";
            } else {
                outStr += str.charAt(i);
            }
        }

        System.out.println(outStr);
This is by far the best solution I've read.
 
Comment

aa1012

Addict
Ganda ng thread na ito. parang stackoverflow vibe. paligsahan ng solution sa tanong. ganyan sana lagi.
 
Comment

homer_simpson

Eternal Poster
C++:
#include <iostream>

int main(void) {
    std::string input;
    std::cout << "Enter input: ";
    std::getline(std::cin, input);
    for (char& c : input) {
        switch (c) {
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                c='*';
                break;
            default:
                break;
        }
        std::cout << c;
    }
    std::cout << std::endl;
    return 0;
}
 
Comment

Like us on facebook

Online statistics

Members online
1,374
Guests online
1,081
Total visitors
2,455

Forum statistics

Threads
883,700
Messages
15,627,274
Members
1,410,698
Top