0

I am writing a program to convert text from a file to piglatin. This is my main()

int main()
{
    FILE* filePtr;
    char inputString[25];
    char pLatinString[25];
    char* Pstringpoint;

    if ((filePtr = fopen("PigLatinIN.txt", "r")) == NULL)
    {
        printf("File could not be opened \n");
    }
    else
    {

        while(fgets(inputString, 25, filePtr)!= NULL)
        {
            Pstringpoint = convertToPigLatin(inputString, pLatinString);
            while(*Pstringpoint != '\0')
            {
                printf("%c", *Pstringpoint++);
            }

        }
    }

this is for an assignment so my conversion to piglatin function has to have this header.

 char* convertToPigLatin(char* engStr, char* pLatinStr)
{
    char completesuffix[25];
    int length = 0;
    char rSuffix[10];
    char vowels[6] = {"aeiou"};
    int i = 0;

    while (engStr[length] != '\0')
    {
        length++;
    }//setting int length equal to the length of the input string. 


    while (i < length)
    {
        for (int j = 0; j < 6; j++)
        {
            //checking if the lead character is a consonant
            if (engStr[0] != vowels[j])
            {
                rSuffix[j] =  "-";
                rSuffix[j+1] = engStr[0];

            }
            //checking if there is a string of consonants
            else if ((i != 0)&&(engStr[i] != vowels[j]))
            {
                rSuffix[j] = engStr[i];
            }
            //appending ay to consonant leading string
            else if ((engStr[i] == vowels[j]) && (engStr[i - 1] != vowels[j]))
            {
                rSuffix[j] = "a";
                rSuffix[j + 1] = "y";
                rSuffix[j + 2] = "\0";

                strcat(engStr, rSuffix);
            }
            //string starting with vowel 
            else if (engStr[0] == vowels[j])
            {
                rSuffix[j] = "-";
                rSuffix[j + 1] = "w";
                rSuffix[j + 2] = "a";
                rSuffix[j + 3] = "y";
                rSuffix[j + 4] = "\0";

                strcat(engStr, rSuffix);
            }

        }//for loop that goes thru the vowel array

        i++;
    }//while loop to go through the input string

    for (int k = 0; k < 25; k++)
    {
        pLatinStr[k] = engStr[k];
    }

    return pLatinStr;

}

the contents of piglatin.txt file are as follows.

go
placidly
amid
the
noise
and
haste
and
remember
what
peace
there
may
be
in
silence

whenever I try to run the program I get a stack corruption error around the variable suffix and I don't know what I am doing wrong.

Aynrand20
  • 3
  • 2
  • 2
    What is your intention with constructs like `char suffix[3] = { "ay\0" };`? The correct way to declare a pre-defined string would look more like `char suffix[3] = "ay";` or `char *suffix = "ay";` When you write `"ay"` in C, C will automatically include a 0 terminator. Your string `"ay\0"` will actually consume 4 characters, which is bigger than what you've allocated. – lurker May 01 '20 at 01:13
  • 1
    `while (!feof(filePtr))`?!? Read this: [**Why is “while ( !feof (file) )” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Andrew Henle May 01 '20 at 01:51
  • (1) In order to use `strcat` on `rSuffix`, you have to first ensure that `rSuffix` is null-terminated, which it does not appear you do, and (2) You cannot return `pLatinStr` after setting it to `rSuffix` via `strcat`. It's the same as `return rSuffix;`, which of course doesn't work since `rSuffix` is a local array that goes out of scope upon return. – Tom Karzes May 01 '20 at 02:08
  • so it would be better to just use the variables I passed into the function since I declared those in main? – Aynrand20 May 01 '20 at 02:23
  • @Aynrand20 I would suggest trying to learn a little about storage classes, pointers, and C strings first. Once you believe you have acquired a rudimentary understanding, then you can revisit your code and hopefully be able to spot the bugs on your own. – Tom Karzes May 01 '20 at 02:49
  • Please include the contents of `PigLatinIN.txt` in your question. – rtx13 May 01 '20 at 04:07
  • @rtx13 I went ahead and updated the question to show my new progress and include the .txt file I am still not able to get it to work properly.. – Aynrand20 May 01 '20 at 05:33
  • @Aynrand20 all of the character replacement statements like `rSuffix[j] = "-";` should be replaced with `rSuffix[j] = '-';`. I also recommend turning on your compiler's warning reporting and reviewing and addressing all of the issues that it reports. – rtx13 May 01 '20 at 05:50

0 Answers0