Advertisement
Guest User

Text or word? :V

a guest
Jun 24th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package texto;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Texto {
  6.  
  7.     static void guion(String texto){
  8.         for(int i = 0; i < texto.length(); i++){
  9.             System.out.print("-");
  10.         }
  11.         System.out.println("");
  12.     }
  13.     static int num_palabras(String texto){
  14.         int c = 1;
  15.         char a[] = new char[texto.length()];
  16.         for(int i = 0; i < texto.length(); i++){
  17.                 a[i] = texto.charAt(i);
  18.                 if(a[i]==' '){
  19.                     c++;
  20.                 }
  21.         }
  22.         return c;
  23.     }
  24.     static void pal_separ(String texto){
  25.         char a[] = new char[texto.length()];
  26.         for(int i = 0; i < texto.length(); i++){
  27.                 a[i] = texto.charAt(i);
  28.                 if(a[i]!=' ')
  29.                     System.out.print(a[i]);
  30.                 else
  31.                     System.out.println("");
  32.         }
  33.        
  34.         System.out.println("");
  35.     }
  36.     static int comp(String str,String texto){
  37.         int c = 0;
  38.         char s = str.charAt(0);
  39.         char a[] = new char[texto.length()];
  40.         for(int i = 0; i < texto.length(); i++){
  41.                 a[i] = texto.charAt(i);
  42.                 if(a[i]==s){
  43.                     c++;
  44.                 }
  45.         }
  46.         return c;
  47.     }
  48.     public static void main(String[] args) {
  49.        
  50.         Scanner s = new Scanner(System.in);
  51.        
  52.         String str;
  53.         boolean f = false;
  54.         System.out.println("Digite un texto o palabra");
  55.         str = s.nextLine();
  56.         char a[] = new char[str.length()];
  57.         for(int i = 0; i < str.length(); i++){
  58.             a[i] = str.charAt(i);
  59.             if(a[i]==' '){
  60.                 f = true; //texto
  61.                 break;
  62.             }
  63.             else{
  64.                 f = false; // palabra
  65.             }
  66.         }
  67.         if(f){
  68.             System.out.println("Digito un texto");
  69.             System.out.println("El numero de palabras es "+num_palabras(str));
  70.             System.out.println("Las palabras presentes en el texto son:");
  71.             pal_separ(str);
  72.             int n = 0;
  73.             do{
  74.                 System.out.println("Digite una letra:");
  75.                 String ch = s.next();
  76.                 if(comp(ch,str)==0){
  77.                     n++;
  78.                     System.out.println("La letra no esta presente");
  79.                 }
  80.                 else
  81.                     System.out.println("La letra "+ch+" aparece "+comp(ch,str)+ " veces.");
  82.             }
  83.             while(n!=2);
  84.         }
  85.         else{
  86.             System.out.println("Digito una palabra");
  87.             System.out.println("Guiones por cada letra:");
  88.             guion(str);
  89.         }
  90.     }
  91.    
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement