字符串指针麻烦,没有正确指向

时间:2012-10-11 15:32:37

标签: c string pointers

这是一个C类。我有以下方法。基本上,如果原始单词以辅音开头,它需要一个单词和一个newWord,通过将第一个字母移动到结尾(在结束标点符号之前)并在字符串中添加“ay”来创建newWord。我的所有逻辑和案例都有效。我遇到的问题是,当我在最后通过char * newWord并打印时,该值仍然是原始单词。 另外,我自己定义了那些isConsonant和isCapital等方法。 EndPunc获取第一个结束标点符号的索引。

实施例: word =“猫?!!” newWord =“Atc?!!”

word =“apple” newWord =“appleay”

但是当我追踪newWord之后,它仍然是“猫?!!”或“苹果”。我做错了什么?

#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;   
}

3 个答案:

答案 0 :(得分: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 */

答案 1 :(得分:0)

使用时:

firstLetter += 32;

您只需为本地变量指定一个值。之后你做了像

这样的事情
newWord = &secondLetter;

获取局部变量的地址并将其分配给newWord。这似乎没有多大意义,因为newWord是你的字符串的地址,也就是字符串的第一个字符的地址。也许你想用

访问你单词的这个地方的字符值

newWord [0] = secondLetter;

你没有在任何地方指定firstLetter和secondLetter的实际值,这就是你没有改变单词的原因。幸运的是,你最终设法用tempWord替换了newWord,所以你再次看到你原来的单词。这个地方不是唯一一个错误的地方,你有很多对newWord的分配看起来真的错了,大部分时间你都在改变指针而不是字符串的底层数据。

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

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

答案 2 :(得分: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;
}