2

这是针对 C 类的。我有以下方法。基本上,它需要一个单词和一个 newWord,如果原始单词以辅音开头,则通过将第一个字母移动到末尾(在结束标点符号之前)并在字符串中添加“ay”来创建 newWord。我所有的逻辑和案例都有效。我遇到的问题是,当我最后通过 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;   
}
4

3 回答 3

1

您正在对纯局部变量进行更改:

    char firstLetter = *tempWord;      /* these are both values not pointers */
    char secondLetter = *(++tempWord); 

更糟糕的是,您正在捕获指向这些局部变量的指针,然后修改它们,而不是您要修改的内存:

    newWord = &firstLetter;
    newWord++;

    /* ... */

    newWord = &secondLetter;
    newWord++;

也许你的意思是:

    *newWord = firstLetter; /* store the letter in the position */
于 2012-10-11T16:05:39.227 回答
0

还有更多建议......但是,修改了您的代码的几个部分;使用文件比较器查看更改...

#include <stdio.h>
//#include <string.h>

#define MAXLENGTH 31

char word[31] = "Aat!\?\?!";
char newWord[31] = {0,};
char* gword = word;
char* gnewWord = 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--))
    while (isPunctuation(*word))
    {
        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;

    // get index of last punctuation, if none, index will be length of string
    int puncIndex = endPuncIndex(length, word);

    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;
            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;
            tempWord++;
            newWord++;
        }

        // add "ay"
        *newWord = 'a';
        newWord++;
        *newWord = 'y';
        newWord++;

    }

    //then add remaining punctuation
    while (puncIndex++ < length)
    {
        *newWord = *tempWord;
        newWord++;
        tempWord++;
    }
    fprintf(stderr, "Modified word: %s", gnewWord);

    return length + 3;
}


int main(void)
{
    pigLatin(gword, gnewWord);
    printf("\n");
    // system("PAUSE");
    return 0;
}
于 2012-10-11T16:16:00.050 回答
0

当您使用:

firstLetter += 32;

您只需为局部变量赋值。之后你会做类似的事情

newWord = &secondLetter;

这将获取局部变量的地址并将其分配给 newWord。这似乎没有多大意义,因为 newWord 是字符串的地址,也就是字符串第一个字符的地址。也许您想在单词的这个位置访问字符的值

newWord[0] = secondLetter;

您没有在任何地方分配 firstLetter 和 secondLetter 的实际值,这就是您的词没有改变的原因。幸运的是,您以某种方式设法在末尾用 tempWord 替换了 newWord,因此您再次在末尾看到了原始单词。这个地方不是唯一一个错误的地方,你有很多对 newWord 的赋值看起来真的是错误的,大多数时候你是在更改指针而不是字符串的底层数据。

我怀疑您是否正确理解了如何使用指针。你应该看看一个很好的教程,例如http://pw1.netcom.com/~tjensen/ptr/pointers.htm

在您熟悉了如何使用指针和字符串之后,您应该查看您的代码并轻松地看到您只更改了指针而不是 newWord 的实际值。

于 2012-10-11T16:02:11.383 回答