145 lines
4.3 KiB
C#
145 lines
4.3 KiB
C#
namespace strings_4;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Menu();
|
|
}
|
|
|
|
static void Menu() {
|
|
Console.Clear();
|
|
int scelta;
|
|
string stringa1, stringa2;
|
|
do {
|
|
Console.WriteLine("Inserire un'opzione:");
|
|
Console.WriteLine("1. Conta spazi pre maiuscola");
|
|
Console.WriteLine("2. Converti in binario");
|
|
Console.WriteLine("3. Stampa a gruppi di 3");
|
|
Console.WriteLine("4. Codifica");
|
|
Console.WriteLine("5. Individua anagramma");
|
|
Console.WriteLine("0. Esci");
|
|
Console.Write("Scelta: ");
|
|
scelta = Convert.ToInt32(Console.ReadLine());
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
Console.Clear();
|
|
Console.WriteLine($"La stringa contiene {ContaSpaziPreMaiuscola(Input())} spazi prima di una maiuscola.");
|
|
Pausa();
|
|
break;
|
|
case 2:
|
|
Console.Clear();
|
|
Console.WriteLine(ToBinary());
|
|
Pausa();
|
|
break;
|
|
case 3:
|
|
Console.Clear();
|
|
GruppiDiTre(Input());
|
|
Pausa();
|
|
break;
|
|
case 4:
|
|
Console.Clear();
|
|
Codifica();
|
|
Pausa();
|
|
break;
|
|
case 5:
|
|
Console.Clear();
|
|
stringa1 = Input();
|
|
stringa2 = Input();
|
|
if (string.Join("", InsertionSort(stringa1.ToLower().ToCharArray())).GetHashCode()
|
|
== string.Join("", InsertionSort(stringa2.ToLower().ToCharArray())).GetHashCode()) {
|
|
Console.WriteLine("Le due stringhe sono anagrammi");
|
|
}
|
|
else {
|
|
Console.WriteLine("Le due stringhe non sono anagrammi");
|
|
}
|
|
Pausa();
|
|
break;
|
|
default:
|
|
Console.WriteLine("Opzione non valida.");
|
|
Pausa();
|
|
break;
|
|
}
|
|
}
|
|
while (scelta != 0);
|
|
}
|
|
|
|
|
|
static void Pausa() {
|
|
Console.WriteLine("Premere un tasto per continuare. . .");
|
|
Console.ReadKey();
|
|
Console.Clear();
|
|
}
|
|
|
|
static string Input() {
|
|
Console.Write("Inserire una frase: ");
|
|
return Console.ReadLine();
|
|
}
|
|
|
|
static int ContaSpaziPreMaiuscola(string p_stringa) {
|
|
int ritorno = 0, i = 0;
|
|
do {
|
|
if (p_stringa.IndexOf(' ') + i == -1) {
|
|
break;
|
|
}
|
|
|
|
if (char.IsUpper(p_stringa[p_stringa.IndexOf(' ') + i])) {
|
|
ritorno++;
|
|
p_stringa = p_stringa.Substring(p_stringa.IndexOf(' ') + i);
|
|
i = 0;
|
|
}
|
|
else {
|
|
i++;
|
|
}
|
|
}
|
|
while (p_stringa.Contains(' ') && p_stringa.IndexOf(' ') + i < p_stringa.Length);
|
|
return ritorno;
|
|
}
|
|
|
|
static string ToBinary() {
|
|
Console.Write("Inserire un numero: ");
|
|
return Convert.ToString(Convert.ToInt32(Console.ReadLine()), 2);
|
|
}
|
|
|
|
static void GruppiDiTre(string p_stringa) {
|
|
for (int i = 0; i < p_stringa.Length; i++) {
|
|
if (i % 3 == 0 && i != 0) {
|
|
Console.Write('-');
|
|
}
|
|
Console.Write(p_stringa[i]);
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
|
|
static void Codifica() {
|
|
string stringa = Input();
|
|
Console.Write("Inserire un numero: ");
|
|
int codice = Convert.ToInt32(Console.ReadLine());
|
|
|
|
for (int i = 0; i < stringa.Length; i++) {
|
|
Console.Write(Convert.ToChar(stringa[i] + codice));
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
|
|
static char[] InsertionSort(char[] p_array)
|
|
{ //implementazione ufficiale copiata da Classroom
|
|
int i, j;
|
|
char temp;
|
|
|
|
for (i = 1; i < p_array.Length; i++)
|
|
{
|
|
temp = p_array[i];
|
|
j = i - 1;
|
|
while (j >= 0 && p_array[j] > temp)
|
|
{
|
|
p_array[j + 1] = p_array[j];
|
|
j--;
|
|
}
|
|
p_array[j + 1] = temp;
|
|
}
|
|
|
|
return p_array;
|
|
}
|
|
} |