转换为Pig Latin

时间:2014-05-06 04:28:18

标签: c++ cstring

将字符串转换为PigLatin需要一些帮助。编写一个程序,输入两个字符串变量,第一个和最后一个,用户应该使用他或她的名字输入每个变量。首先,将两个字符串转换为全部大写。然后,您的程序应该创建一个新的字符串,其中包含pig latin中的全名。 这是我的主要功能:

char* first = new char[MAX];
char* last = new char[MAX];
char* full = new char[MAX*2];

cout << "Enter first name: ";
cin.getline(first, MAX, '\n');
cout << "Enter last name: ";
cin.getline(last, MAX, '\n');

for(int i = 0; first[i]; i++)
{
first[i] = toupper(first[i]);
}
for(int i = 0; last[i]; i++)
{
last[i] = toupper(last[i]);
}

transformPigLatin(first);

transformPigLatin(last);

int offset = 0;
    int i;

    for (i=0; i<MAX && first[i]!=0; i++)
        full[offset++]=first[i];
    full[offset++]=' ';
    for (i=0; i<MAX && last[i]!=0; i++)
        full[offset++]=last[i];


transformPigLatin(full);
cout << full << endl;

和piglatin func(只需要帮助这个功能):

char* transformPigLatin(char* word)
{
int length = strlen(word);
char ch;
for (int i = 0; i < length; i++){

if((length > 1) && (ch == 'a' || ch == 'A' ||ch =='e'||ch =='E'||ch =='i'||ch == 'I'||ch =='o'||ch =='O'||ch =='u'||ch =='U'))

{
strcat(word, "WAY");
}
else
{
    strcat(word, "AY");
}
return word;
}
}

这两件事我无法弄清楚如何处理c-string:

1)如果第一个字母是辅音,则将其移至末尾并在末尾添加“ay”。 2)如果第一个字母是元音,请在末尾添加“way”。

任何提示将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

您使用C ++进行编码,因此您应该使用C ++字符串而不是C字符串。使用C ++字符串可以轻松实现。 std::string类有许多方法可以处理字符串内容,包括erase()append()

试试这个:

#include <string>
#include <algorithm>

std::string first;
std::string last;
std::string full;

std::cout << "Enter first name: ";
std::getline(std::cin, first);
std::cout << "Enter last name: ";
std::getline(std::cin, last);

std::transform(first.begin(), first.end(), first.begin(), ::toupper);
std::transform(last.begin(), last.end(), last.begin(), ::toupper);

transformPigLatin(first);
transformPigLatin(last);

full = first + " " + last;
std::cout << full << std::endl;

void transformPigLatin(std::string &word)
{
    if (word.empty()) return;

    char ch = word[0];
    if ((ch < 'A') || (ch > 'Z')) return;

    if (std::string("AEIOU").find(ch) != std::string::npos)
    {
        word += "WAY";
    }
    else
    {
        if (word.length() > 1)
        {
             word.erase(0, 1);
             word.append(&ch, 1);
        }
        word += "AY";
    }
}

更新:如果您必须仅使用C字符串,请尝试更类似的内容:

int getLine(char *prompt, char *word, int maxlen)
{
    if (prompt != NULL)
    {
        printf("%s", prompt);
        fflush(stdout);
    }

    if (fgets(word, maxlen, stdin) == NULL)
    {
        *word = '\0';
        return 0;
    }

    int length = strlen(word);

    if (word[length-1] == '\n')
    {
        word[length-1] = '\0';
        --length;
    }
    else
    {
        int ch;
        while (((ch = getchar()) != '\n') && (ch != EOF));
    }

    return length;
}

char* first = new char[MAX];
char* last = new char[MAX];
char* full = new char[(MAX*2)+1];

getLine("Enter first name: ", first, MAX);
getLine("Enter last name: ", last, MAX); 

for(int i = 0; first[i]; i++)
{
    first[i] = toupper(first[i]);
}

for(int i = 0; last[i]; i++)
{
    last[i] = toupper(last[i]);
}

transformPigLatin(first);
transformPigLatin(last);

strcpy(full, first);
strcat(full, " ");
strcat(full, last);

printf("%s\n", full);

void transformPigLatin(char* word)
{
    int length = strlen(word);
    if (length == 0) return;

    char ch = word[0];
    if ((ch < 'A') || (ch > 'Z')) return;

    if (strchr("AEIOU", ch) != NULL)
    {
        if ((length+3) >= MAX) return;
        strcat(word, "WAY");
    }
    else
    {
        if ((length+2) >= MAX) return;
        if (length > 1)
        {
            for (int i = 1; i < length; ++i)
                word[i-1] = word[i];
            word[length-1] = ch;
        }
        strcat(word, "AY");
    }
}