School-Coding-Cpp/sfusi/encrypt_pigLatinLanguage.cpp

77 lines
1.5 KiB
C++

/*
Nome: Mario
Cognome: Montanari
pigLatin
*/
#include <iostream>
#include <cstring>
#define SIZE 100+1
using namespace std;
bool isVowel(char chr);
void pigLatin(char *mssg);
int main(void) {
char mssg[SIZE];
cout << "Inserisci una frase: ";
cin.getline(mssg, SIZE);
cout << "Frase codificata: ";
pigLatin(mssg);
return 0;
}
bool isVowel(char chr) {
return (
chr == 'A' || chr == 'a' ||
chr == 'E' || chr == 'e' ||
chr == 'I' || chr == 'i' ||
chr == 'O' || chr == 'o' ||
chr == 'U' || chr == 'u'
);
}
void pigLatin(char *mssg) {
char *token = strtok(mssg, " ");
while (token != nullptr) {
int len = strlen(token);
bool hasPunctuation = ispunct(token[len - 1]);
char punctuation = '\0';
if (hasPunctuation) {
punctuation = token[len - 1];
token[len - 1] = '\0';
len--;
}
if (isVowel(token[0])) {
cout << token << "way";
} else {
char first = token[0];
for (int i = 0; i < len - 1; i++) {
token[i] = token[i + 1];
}
token[len - 1] = first;
token[len] = '\0';
cout << token << "ay";
}
if (hasPunctuation) {
cout << punctuation;
}
cout << " ";
token = strtok(nullptr, " ");
}
cout << endl;
}