2012-10-11 53 views
2

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

例如: word =“Cat?!!” newWord =“Atc?!!”

字= “苹果” 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; 
} 
+0

C没有类。 –

+1

你能分享完整的代码吗?显然在分配中没有使用字符串函数。 – SparKot

+0

我的意思是说,这是我正在注册的一个班级。 – user1738539

回答

0

当你使用:

firstLetter += 32; 

你只值分配给本地变量。之后,你做的东西一样

newWord = &secondLetter; 

这需要局部变量的地址,并将其分配给刷;这似乎没有多大意义,因为newWord是字符串的地址,也就是字符串的第一个字符的地址。也许你想访问在这个地方的字符的值与

newWord [0] = secondLetter;

你没有分配任何地方的firstLetter和secondLetter的实际值,这就是为什么你的话没有改变。幸运的是,你不知何故设法用最终的临时词替代新词,所以你再次看到你的原始单词。这个地方不是唯一一个错误的地方,你有很多NewWord的分配看起来确实是错误的,大部分时间你改变指针而不是字符串的底层数据。

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

在熟悉了如何使用指针和字符串之后,您应该查看自己的代码,并且很容易看到只改变了指针而不是newWord的实际值。

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 */ 
+0

哦,我现在明白了。 * newWord = * tempWord会将tempWord中的字符分配给newWord。在newWord = tempWord的时候,会设置newWord指向tempWord指向的是什么? – user1738539

+0

YES = D你懂了! –

0

还有更多的建议......但是,MODI在你的代码中添加了几个部分;使用文件比较器查看更改...

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