これはCクラス用です。私は次の方法を持っています。元の単語が子音で始まる場合は、最初の文字を末尾 (句読点を終了する前) に移動して newWord を作成し、文字列に「ay」を追加します。これに対する私のすべてのロジックとケースはうまくいきます。私が抱えている問題は、最後に char* newWord を通過して印刷すると、値が元の単語のままになることです。また、isConsonant や isCapital などのメソッドを自分で定義しました。EndPunc は、最初の終了句読点のインデックスを取得します。
例: word = "猫?!!" newWord = "Atc?!!"
word = "りんご" newWord = "appleay"
しかし、後で newWord をトラバースすると、まだ「Cat?!!」です。または「リンゴ」。私は何を間違っていますか?
#include <stdio.h>
//#include <string.h>
#define MAXLENGTH 31
char word[31] = "Aat!??!";
char newWord[31] = "";
char* w = word; char* n = newWord;
int isConsonant(char c) // return 1 if consonant, 0 if vowel, -1 if neither
{
int i = 0;
while(i < 33)
{
if(c == 'a'-i || c == 'e'-i || c == 'i'-i || c == 'o'-i || c == 'u'-i)
return 0;
i+=32;
}
if(c >= 65 && c <= 122) // its a letter
return 1;
else return -1;
}
int isCapital(char c)
{
if(c >= 97 && c <= 122) // lowerCase
return 0;
else return 1; // capital
}
int isPunctuation(char c)
{
if(c == '!' || c == ',' || c == '.' || c == '?' || c == ';' || c == ':')
return 1;
return 0;
}
int endPuncIndex(int wordLength, char* word)
{
int index = wordLength;
word += wordLength-1;
while(isPunctuation(*word--))
index--;
return index;
}
int pigLatin(char* word, char* newWord)
{
int length = 0;
char* tempWord = word;
while(*tempWord++ != '\0')
if(++length > MAXLENGTH)
return -1;
tempWord = tempWord-length-1;
int puncIndex = endPuncIndex(length, word); // get index of last punctuation, if none, index will be length of string
if(isConsonant(*word) == 1) // first letter is consonant
{
char firstLetter = *tempWord;
char secondLetter = *(++tempWord);
tempWord++;
if(isCapital(firstLetter))
{
firstLetter += 32; // makes it lowercase, will need to move this to the end
if(isCapital(secondLetter) == 0) // if second letter isn't capital, make it capital
secondLetter -= 32;
}
int start = 0;
newWord = &secondLetter;
newWord++;
while(start++ < puncIndex-2) // go up to punct index (or end of String if no ending punct)
{
newWord = tempWord++;
newWord++;
}
newWord = &firstLetter;
newWord++;
}
else // vowel, just copies the word letter for letter, no insert or shifting
{
int start = 0;
while(start++ < puncIndex) // go up to punct index (or end of String if no ending punct)
{
newWord = tempWord++;
newWord++;
}
}
// add "ay"
newWord = "a";
newWord++;
newWord = "y";
newWord++;
//then add remaining punctuation
while(puncIndex++ < length)
{
newWord = tempWord++;
newWord++;
}
newWord = newWord-(length);
while(*newWord != '\0')
printf("%c",*(newWord++));
return length+3;
}
int main()
{
pigLatin(w,n);
printf("\n");
system("PAUSE");
return 0;
}