Advertisement
fcamuso

Untitled

Jun 5th, 2020
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace conta_vocali_consonanti_punteggiatura
  5. {
  6.   class Program
  7.   {
  8.     static void Main(string[] args)
  9.     {
  10.       //Console.WriteLine("Inserisci una frase");
  11.       //string lettura = Console.ReadLine();
  12.  
  13.       //int vocali = 0, consonanti = 0, punteggiatura = 0, cifre=0;
  14.  
  15.  
  16.       //S E N Z A  A R R A Y
  17.  
  18.       //foreach (char c in lettura)
  19.       //  switch (char.ToLower(c))
  20.       //  {
  21.       //    case 'a': case 'e': case 'i': case 'o': case 'u':
  22.       //      vocali++;
  23.       //    break;
  24.  
  25.       //    case '.': case ',': case ' ': case ';': case '!':
  26.       //      punteggiatura++;
  27.       //    break;
  28.  
  29.       //    case '0': case '1': case '2': case '3': case '4': case '5':
  30.       //    case '6': case '7': case '8': case '9':
  31.       //      cifre++;
  32.       //      break;
  33.  
  34.       //    default:
  35.       //      consonanti++;
  36.       //    break;
  37.       //  }
  38.  
  39.       //foreach (char c in lettura)
  40.       //{
  41.       //  if ("aeiou".Contains(char.ToLower(c)))
  42.       //    vocali++;
  43.       //  else
  44.       //    if ("!?.,:; /{}\"".Contains(char.ToLower(c)))
  45.       //      punteggiatura++;
  46.       //    else
  47.       //      if ("bcdfghjklmnpqrstvwxyz".Contains(char.ToLower(c)))
  48.       //        consonanti++;
  49.       //      else
  50.       //        if ("0123456789".Contains(char.ToLower(c)))
  51.       //          cifre++;
  52.       //}
  53.  
  54.       //foreach (char c in lettura)
  55.       //{
  56.       //  if ("aeiou".Contains(char.ToLower(c)))
  57.       //    vocali++;
  58.       //  else
  59.       //    if (char.IsPunctuation(c))
  60.       //    punteggiatura++;
  61.       //  else
  62.       //    if (char.IsLetter(c))
  63.       //      consonanti++;
  64.       //    else
  65.       //       if (char.IsDigit(c))
  66.       //        cifre++;
  67.       //}
  68.       //Console.WriteLine($"Vocali: {vocali}, Consonanti: {consonanti}, " +
  69.       //                  $"Punteggiatura: {punteggiatura}, Cifre: {cifre}");
  70.  
  71.       // UN ALTRO ESERCIZIO
  72.       //DirectoryInfo cartella = new DirectoryInfo(".");
  73.       //long totale_spazio = 0;
  74.  
  75.       //foreach (FileInfo file in cartella.GetFiles())
  76.       //{
  77.       //  Console.WriteLine($"File: {file.Name.PadRight(60, ' ')} - Dimensione: {file.Length}bytes");
  78.       //  totale_spazio += file.Length;
  79.       //}
  80.  
  81.       //Console.WriteLine($"\n----------Totale spazio occupato dalla cartella:  {totale_spazio}bytes");
  82.  
  83.  
  84.       //C O N T A   O C C O R R E N Z E - V E R S O  G L I  A R R A Y
  85.  
  86.       //La soluzione del masochista
  87.       //int a=0, b=0, c=0, d=0; //ecc. abbiate pieta!
  88.  
  89.       Console.WriteLine("Inserisci una frase");
  90.       string lettura = Console.ReadLine();
  91.  
  92.       //foreach (char lettera in lettura)
  93.       //  switch (lettera)
  94.       //  {
  95.       //    case 'a': a++; break;
  96.       //    case 'b': b++; break;
  97.       //    case 'c': c++; break;
  98.       //    case 'd': d++; break;
  99.       //  }
  100.  
  101.       //con le stringe - variante 1
  102.       //string alfabeto = "abcdefghkilmnopqrstuvwxyz01234567890-=+_.,?()%$^&*/";
  103.  
  104.       //foreach(char lettera in alfabeto)
  105.       //{
  106.       //  int occorrenze = 0;
  107.  
  108.       //  foreach (char c in lettura)
  109.       //    if (c == lettera) occorrenze++;
  110.  
  111.       //  if (occorrenze > 0)
  112.       //    Console.WriteLine($"Simbolo {lettera} trovato {occorrenze} volt{(occorrenze>1 ? 'e' : 'a')}");
  113.       //}
  114.  
  115.       string alfabeto = "abcdefghkilmnopqrstuvwxyz01234567890-=+_.,?()%$^&*/";
  116.  
  117.       string risultato = "";
  118.  
  119.       foreach (char simbolo in alfabeto)
  120.       {
  121.         int occorrenze = 0;
  122.  
  123.         foreach (char c in lettura)
  124.           if (c == simbolo) occorrenze++;
  125.  
  126.         if (occorrenze > 0)
  127.         {
  128.           Console.WriteLine($"Simbolo {simbolo} trovato {occorrenze} volt{(occorrenze > 1 ? 'e' : 'a')}");
  129.           risultato += simbolo + occorrenze.ToString().PadLeft(5, '0');
  130.         }
  131.       }
  132.       Console.WriteLine(risultato);
  133.  
  134.       char cercato = 'c';
  135.       for (int i = 0; i<risultato.Length/6; i++)
  136.       {
  137.         string blocco = risultato.Substring(6 * i, 6);
  138.  
  139.         if (blocco[0] == cercato)
  140.         {
  141.           int occorrenze = int.Parse(blocco.Substring(1, 5));
  142.           Console.WriteLine($"Simbolo {cercato} trovato {occorrenze} volt{(occorrenze > 1 ? 'e' : 'a')}");
  143.           break;
  144.         }
  145.      }
  146.  
  147.  
  148.  
  149.  
  150.     }
  151.   }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement