137 lines
3.9 KiB
C#
137 lines
3.9 KiB
C#
namespace strings_3;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Menu();
|
|
}
|
|
|
|
static void Menu() {
|
|
Console.Clear();
|
|
int scelta;
|
|
string stringa;
|
|
do {
|
|
Console.WriteLine("Inserire un'opzione:");
|
|
Console.WriteLine("1. Alterna i caratteri");
|
|
Console.WriteLine("2. Codifica di una frase");
|
|
Console.WriteLine("3. Primo carattere maiuscolo");
|
|
Console.WriteLine("4. Sostituisci sottostringa");
|
|
Console.WriteLine("5. Parola palindroma pari");
|
|
Console.WriteLine("5. Parola palindroma dispari");
|
|
Console.WriteLine("0. Esci");
|
|
Console.Write("Scelta: ");
|
|
scelta = Convert.ToInt32(Console.ReadLine());
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
Console.Clear();
|
|
AlternaCaratteri();
|
|
Pausa();
|
|
break;
|
|
case 2:
|
|
Console.Clear();
|
|
Console.WriteLine(Codifica());
|
|
Pausa();
|
|
break;
|
|
case 3:
|
|
Console.Clear();
|
|
//ContaStringa();
|
|
Pausa();
|
|
break;
|
|
case 4:
|
|
Console.Clear();
|
|
//Input4();
|
|
Pausa();
|
|
break;
|
|
case 5:
|
|
Console.Clear();
|
|
//PariEDispari();
|
|
Pausa();
|
|
break;
|
|
case 6:
|
|
Console.Clear();
|
|
//PariEDispari();
|
|
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 void AlternaCaratteri() {
|
|
string stringa1 = Input();
|
|
string stringa2 = Input();
|
|
bool stringa1Max = stringa1.Length > stringa2.Length;
|
|
int lunghezza = stringa1Max ? stringa1.Length : stringa2.Length;
|
|
|
|
for (int i = 0; i < lunghezza; i++) {
|
|
if (stringa1Max) {
|
|
if (i < stringa2.Length) {
|
|
Console.Write($"{stringa1[i]}{stringa2[i]}");
|
|
}
|
|
else {
|
|
Console.Write(stringa1[i]);
|
|
}
|
|
}
|
|
else {
|
|
if (i < stringa1.Length) {
|
|
Console.Write($"{stringa1[i]}{stringa2[i]}");
|
|
}
|
|
else {
|
|
Console.Write(stringa2[i]);
|
|
}
|
|
}
|
|
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
|
|
static string Codifica() {
|
|
string stringa = Input();
|
|
char codice = 'f';
|
|
const char inutile = '\\';
|
|
char[] ritorno = new char[stringa.Length * 2];
|
|
Array.Fill(ritorno, inutile);
|
|
char[] vocali = { 'a', 'e', 'i', 'o', 'u' };
|
|
int k = 0;
|
|
|
|
|
|
for (int i = 0; i < stringa.Length; i++) {
|
|
ritorno[k] = stringa[i];
|
|
for (int j = 0; j < vocali.Length; j++) {
|
|
if (stringa[i] == vocali[j]) {
|
|
k++;
|
|
ritorno[k] = codice;
|
|
break;
|
|
}
|
|
else if (stringa[i] == char.ToUpper(vocali[j])) {
|
|
k++;
|
|
ritorno[k] = char.ToUpper(codice);
|
|
break;
|
|
}
|
|
}
|
|
k++;
|
|
|
|
}
|
|
|
|
return string.Join("", ritorno).Split(inutile).First();
|
|
}
|
|
}
|