Вот реализация, которая должна работать:
void doStuff(void);
int startsWithVowel(char c);
int isALetter(char c);
void doStuff(){
int processingWord = 0;
int already_latin = 0;
char c = 0;
char first_letter = 0;
while(read(fileRead, &c, sizeof(c)) > 0) {
if(processingWord == 0) {
processingWord = 1;
if(!startsWithVowel(c)){ //append constants to the end of the word in pig latin *EDIT*
first_letter = c;
continue;//Here we do not fall through and write
}
}
else{
if(isALetter(c)){ //This is the general case of just writing the read character
write(fileWrite, &c, sizeof(c));
}
else if(c != '\n'){ //Here is handling for , and . special characters
if(isALetter(first_letter)){ //we hit a . or , with a vower word, need to add first letter then "ray"
write(fileWrite, &first_letter, sizeof(first_letter));
}
write(fileWrite, "ray", sizeof("ray"));
write(fileWrite, &c, sizeof(c));
already_latin = 1;
}
else if(c == '\n') { //here is the end of the string
if(isALetter(first_letter)){
write(fileWrite, &first_letter, sizeof(first_letter));
}
if(!already_latin){
write(fileWrite, "ray", sizeof("ray"));
}
write(fileWrite, &c, sizeof(c));
processingWord = 0; //reset all the flags for the next word.
first_letter = 0;
already_latin = 0;
}//end of '\n'
}//end of if/else block
}//end of while loop
}//end of function
/* =========================================================
return true (1) if the character is a vowel and 0 otherwise
============================================================ */
int startsWithVowel(char c){
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
return 1;
}
return 0;
}
/* =========================================================
return true (1) if the character is a letter and 0 otherwise
============================================================ */
int isALetter(char c){
if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))){
return 1;
}
return 0;
}
Есть еще куча неиспользуемых вещей, таких как переменная bytes, и все может быть чище, но это должно работать так, как вам нужно. Попробуйте запустить его и дайте мне знать, как оно идет, если я все еще здесь, я обновлю какие-либо ошибки сегодня вечером
EDIT
Похоже, я сделал это задом наперед, меняя только гласные (вместо констант). моя свинья латынь ржавая.
Хорошо, я сделал локальную строку для проверки синтаксического анализа в сети с codechef.com/ide, вы можете скопировать и вставить ее туда, чтобы проверить. Измените printfs на записи, которые имитируют printfs, и я думаю, что вы готовы идти:
#include <stdio.h>
#include <string.h>
void doStuff(void);
int startsWithVowel(char c);
int isALetter(char c);
char * str = "It\nis\nthe\nZucca\nGigantopithecus,\nor\nGreat\nPumpkin,\nCharlie\nBrown.";
int main(void) {
doStuff();
return 0;
}
void doStuff(){
int processingWord = 0;
char c = 0;
char first_letter = 0;
int already_latin = 0;
//while(read(fileRead, &c, sizeof(c)) > 0) {
while(strlen(str) > 0){ //Made local for testing, no file io here
c = str[0];
str++; //end of local nonsense you wont have to use
if(processingWord == 0) {
processingWord = 1;
if(!startsWithVowel(c)){
first_letter = c;
continue;//Here we don not fall through and write
}
}
if(processingWord == 1) {
if(isALetter(c)){ //This is the general case of just writing the read character
//write(fileWrite, &c, sizeof(c));
printf("%c",c);
//printf(" SHOULD PRINT FIRST LETTER VOWEL HERE ");
}
else if(c != '\n'){ //Here is handling for , and . special characters
if(isALetter(first_letter)){ //we hit a . or , with a vower word, need to add first letter then "ray"
//write(fileWrite, &first_letter, sizeof(first_letter));
printf("%cay%c",first_letter,c);
}
else{
//write(fileWrite, "ray", sizeof("ray"));
//write(fileWrite, &c, sizeof(c));
printf("ray%c", c);
}
already_latin = 1;
}
else if(c == '\n') { //here is the end of the string
if(!already_latin){
if(isALetter(first_letter)){
//write(fileWrite, &first_letter, sizeof(first_letter));
printf("%cay",first_letter);
//printf(" SHOULD PRINT FIRST LETTER CONSTANT HERE ");
}
else{
//write(fileWrite, "ray", sizeof("ray"));
printf("ray");
}
}
//write(fileWrite, &c, sizeof(c));
printf("%c", c);
processingWord = 0;
first_letter = 0;
already_latin = 0;
}//end of '\n'
}//end of if/else block
}//end of while loop
}//end of function
/* =========================================================
return true (1) if the character is a vowel and 0 otherwise
============================================================ */
int startsWithVowel(char c){
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
return 1;
}
return 0;
}
/* =========================================================
return true (1) if the character is a letter and 0 otherwise
============================================================ */
int isALetter(char c){
if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))){
return 1;
}
return 0;
}
ВЫВОД:
Itray
isray
hetay
uccaZay
igantopithecusGay,
orray
reatGay
umpkinPay,
harlieCay
rownBay.