215 lines
6.5 KiB
C#
215 lines
6.5 KiB
C#
namespace strings_3;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
Menu();
|
|
}
|
|
|
|
static void Menu() {
|
|
Console.Clear();
|
|
int scelta;
|
|
const int LUNGHEZZA = 100;
|
|
string stringa1, stringa2;
|
|
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("6. Parola palindroma dispari");
|
|
Console.WriteLine("0. Esci");
|
|
Console.Write("Scelta: ");
|
|
scelta = Convert.ToInt32(Console.ReadLine());
|
|
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
Console.Clear();
|
|
stringa1 = Input();
|
|
stringa2 = Input();
|
|
if (Controllo1(stringa1, stringa2)) {
|
|
Console.WriteLine(AlternaCaratteri(stringa1, stringa2));
|
|
}
|
|
else {
|
|
Console.WriteLine("Errore: le due stringhe devono essere lunghe uguali");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 2:
|
|
Console.Clear();
|
|
Console.WriteLine(Codifica());
|
|
Pausa();
|
|
break;
|
|
case 3:
|
|
Console.Clear();
|
|
stringa1 = Input();
|
|
|
|
if (Controllo2(stringa1, LUNGHEZZA)) {
|
|
Console.WriteLine(PrimoCarattereMaiuscolo(stringa1));
|
|
}
|
|
else {
|
|
Console.WriteLine($"Errore: la frase non può essere più lunga di {LUNGHEZZA} caratteri");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 4:
|
|
Console.Clear();
|
|
Console.WriteLine("È richiesta la frase in cui effettuare la ricerca");
|
|
stringa1 = Input();
|
|
Console.WriteLine();
|
|
Console.WriteLine("È richiesta la frase che verrà sostituita");
|
|
stringa2 = Input();
|
|
Console.WriteLine();
|
|
if (Controllo2(stringa2, stringa1.Length)) {
|
|
Console.WriteLine(TrovaESostituisci(stringa1, stringa2));
|
|
}
|
|
else {
|
|
Console.WriteLine($"Errore: la frase da cercare non può essere più lunga della frase in cui cercare");
|
|
}
|
|
Pausa();
|
|
break;
|
|
case 5:
|
|
Console.Clear();
|
|
Console.WriteLine(CreaPalindromo(Input(), true));
|
|
Pausa();
|
|
break;
|
|
case 6:
|
|
Console.Clear();
|
|
Console.WriteLine(CreaPalindromo(Input(), false));
|
|
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 bool Controllo1(string p_stringa1, string p_stringa2) {
|
|
bool ritorno;
|
|
if (p_stringa1.Length == p_stringa2.Length) {
|
|
ritorno = true;
|
|
}
|
|
else {
|
|
ritorno = false;
|
|
}
|
|
|
|
return ritorno;
|
|
|
|
}
|
|
|
|
static string AlternaCaratteri(string p_stringa1, string p_stringa2) {
|
|
string ritorno = "";
|
|
|
|
for (int i = 0; i < p_stringa1.Length; i++) {
|
|
ritorno = string.Concat(ritorno, p_stringa1[i], p_stringa2[i]);
|
|
}
|
|
|
|
return ritorno;
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
static bool Controllo2(string p_stringa1, int p_lunghezza) {
|
|
bool ritorno;
|
|
if (p_stringa1.Length < p_lunghezza) {
|
|
ritorno = true;
|
|
}
|
|
else {
|
|
ritorno = false;
|
|
}
|
|
|
|
return ritorno;
|
|
|
|
}
|
|
|
|
static string PrimoCarattereMaiuscolo(string p_stringa) {
|
|
string[] substrings = p_stringa.Split();
|
|
|
|
for (int i = 0; i < substrings.Length; i++) {
|
|
substrings[i] = char.ToUpper(substrings[i][0]) + substrings[i].Substring(1).ToLower();
|
|
}
|
|
|
|
return string.Join(" ", substrings);
|
|
}
|
|
|
|
static string TrovaESostituisci(string p_stringa1, string p_stringa2) {
|
|
string ritorno;
|
|
const string sostituisci = "*";
|
|
|
|
if (p_stringa1.Contains(p_stringa2)) {
|
|
ritorno = p_stringa1;
|
|
for (int i = 0; i < p_stringa2.Length; i++) {
|
|
ritorno = ritorno.Replace(p_stringa2[i], Convert.ToChar(sostituisci));
|
|
}
|
|
|
|
}
|
|
else {
|
|
ritorno = $"Nessuna occorrenza di {p_stringa2} trovata in {p_stringa1}";
|
|
}
|
|
|
|
return ritorno;
|
|
}
|
|
|
|
static string CreaPalindromo(string p_stringa, bool isPari) {
|
|
string ritorno = p_stringa;
|
|
int partenza;
|
|
if (isPari) {
|
|
partenza = p_stringa.Length - 1;
|
|
}
|
|
else {
|
|
partenza = p_stringa.Length - 2;
|
|
}
|
|
for (int i = partenza; i > -1; i--) {
|
|
ritorno = ritorno + p_stringa[i];
|
|
}
|
|
|
|
return ritorno;
|
|
}
|
|
}
|