2

This is for a C class. I have the following method. Basically, it takes a word, and a newWord, if the original word starts with a consonant, make newWord by moving first letter to the end (before ending punctuation) and add "ay" to the string. All my logic and cases for this works. The problem I'm having is that when I go through char* newWord at the end and print, the value is still the original word. Also, I defined those isConsonant and isCapital, etc methods myself. EndPunc gets the index of the first ending punctuation.

Example: word = "Cat?!!" newWord = "Atc?!!"

word = "apple" newWord = "appleay"

But when I traverse newWord after, it's still "Cat?!!" or "apple". What am I doing wrong?

#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
  • 1
    Can you share the complete code? Apparently no string functions were used in the assignment. Oct 11, 2012 at 15:36
  • I mean it's for a class that I am currently enrolled in. Oct 11, 2012 at 15:44
  • I added the full code that will compile Oct 11, 2012 at 15:46

3 Answers 3

1

You're making changes to purely local variables:

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

Worse, you're capturing pointers to these local variables and then modifying them rather than the memory you were looking to modify:

    newWord = &firstLetter;
    newWord++;

    /* ... */

    newWord = &secondLetter;
    newWord++;

Perhaps you meant:

    *newWord = firstLetter; /* store the letter in the position */
1
  • Oh, I get it now. *newWord = *tempWord would assign the character in tempWord to newWord. Where as newWord = tempWord, would set newWord to point to what tempWord points to right? Oct 11, 2012 at 16:18
0

When you use:

firstLetter += 32;

you only assign a value to your local variable. After that you do stuff like

newWord = &secondLetter;

That takes the address of the local variable and assigns it to the newWord. That doesnt seem to make much sense, because newWord is the adress of your string aka the address of the first character of the string. Maybe you wanted to access the value of character at this place of your word with

newWord[0] = secondLetter;

You did not assign the actual value of the firstLetter and secondLetter anywhere, thats why you word didn't change. Fortunately you somehow managed to replace the newWord with the tempWord at the end, so you see your original word at the end again. This place is not the only one that is wrong, you have a lot of assignments to newWord that look really wrong, most of the time you are changing the pointer instead of the underlying data of the string.

I doubt that you correctly understood how to use pointers. You should take a look at a good tutorial, e.g. http://pw1.netcom.com/~tjensen/ptr/pointers.htm

After you got familiar how to use pointer and strings, you should review your code and easily see that you only change pointers instead of the actual values of newWord.

0

There are many more to suggest... however, modified few parts of your code; see the changes using a file comparator...

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.