Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Escriba un codigo que pida una frase, y el programa te diga cuantas
- * palabras contiene. */
- import java.util.*;
- public class cadenas05{
- public static void main (String args[]) {
- String f = validar("una");
- int contador = verificar(f);
- if (contador==1){
- System.out.println("La frase contiene "+contador+" palabra.");}
- else{
- System.out.println("La frase contiene "+contador+" palabras.");}
- }
- public static String validar(String X){
- Scanner tex = new Scanner(System.in);
- System.out.print("Ingrese "+X+" frase: ");
- String frase = tex.nextLine();
- while (frase.length()==0){
- System.out.println("ERROR, frase nula.");
- System.out.print("Ingrese "+X+" frase: ");
- frase = tex.nextLine();
- }return frase;
- }
- public static int verificar(String A){
- int largo = A.length()-1;
- int contador = 1;
- for (int X = 0;X<=largo;X++){
- if ((A.charAt(X)==' ')&&(A.charAt(X+1)!=' ')){
- contador++;
- }}return contador;
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement