0
**Source.c**

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

#define SIZE 80
#define SPACE ' '

extern int isVowel(char);
extern void initialiseString(char[]);
extern int copyString(char[], char[], int);

int main() {
    char originalText[SIZE] = {'\0'};
    char piglatinText[SIZE] = {'\0'};
    char firstChar[SIZE];
    int i, j, k, l, wordCount;
    wordCount = 1;
    i = j = k = l = 0;

    printf("Enter the text for which you want to generate the piglatin\n");
    gets(originalText);

    while (originalText[i] != NULL) {
        if (originalText[i] == SPACE) {
            if (originalText[i + 1] != SPACE) {
                wordCount++;
            }
        }
        i++;
    }

    printf("Total words in the string are %i\n", wordCount);

    // piglatin Generator
    for (i = 0; i < wordCount; i++) {
        initialiseString(firstChar);
        l = 0;

        while (!isVowel(originalText[j]) && (originalText[j] != SPACE) && (originalText[j] != NULL)) {
            firstChar[l++] = originalText[j++];
        }

        if (isVowel(originalText[j])) {
            while ((originalText[j] != SPACE) && (originalText[j] != NULL)) {
                piglatinText[k++] = originalText[j++];
            }

            k = copyString(piglatinText, firstChar, k);
        } else {
            firstChar[l] = '\0';
            k = copyString(piglatinText, firstChar, k);
        }

        piglatinText[k++] = 'a';
        piglatinText[k++] = ' ';
        j++;
    }

    printf("The piglatin text\n");
    puts(piglatinText);

    getch();
    return EXIT_SUCCESS;
}

Functions.c

#include <string.h>

int isVowel(char ch) {
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
        return 1;
    }

    return 0;
}

void initialiseString(char string[]) {
    int i;
    int len;
    len = strlen(string);
    i = 0;

    while (i <= len) {
        string[i++] = '\0';
    }

    return;
}

int copyString(char stringOne[], char stringTwo[], int k) {
    int i;
    int len;
    len = strlen(stringTwo);
    i = 0;

    while (len > 0) {
        stringOne[k++] = stringTwo[i++];
        len--;
    }

    return k;
}

Here the main function is present in the Source.c file and all the functions used in Source.c file are defined in Functions.c file.

Whenever I run this code, the originalText is encoded correctly but at the end the error stack around the variable 'PiglatinText' was corrupted is generated! I tried to find the error by debugging but was unable to find the error source.

Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • 1
    Can't run it bc. you use conio.h and I'm on Linux. But there are several problems with this. You have to be very careful with string length checking, especially with user input. Fore example, gets() doesn't care about your buffer size and will happily read 80+ chars. But i suspect the error in copyString. Print the end value of k. You never check if k < SIZE -- Edit: gcc gives me tons of warnings. Always compile with warning flags and correct them! – Benjamin Maurer Feb 18 '15 at 10:01
  • Your new string may be longer than `SIZE`. ([Don't use `gets`, by the way.](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used)) – Jongware Feb 18 '15 at 10:05
  • First of all thank you Benjamin for answering my question. Now about the k, I didn't include the tests for k intentionally and that's because I never type too long strings. I excluded the test for k just for my convenience. About conio.h, you can remove it but than you will have to remove the getch() function, called just before the return EXIT_SUCCESS. I just can't figure out why is the stack around piglatinText being corrupted! – Gurucharan Sharma Feb 18 '15 at 10:05
  • The program actually works for me, unless I enter too much text. – Benjamin Maurer Feb 18 '15 at 10:06

1 Answers1

1

To your void initialiseString(char string[]) function you are passing a non-initialized array firstChar[SIZE]. And in that function you are using strlen() on that array. This will cause a undefined behavior.

Most probably this function giving you a wrong length and based on that you are wrongly initializing it. This may lead to stack corruption on further processing.

You should first initialize firstChar[SIZE] with null characters as you have done with other arrays.

// piglatin Generator
    for (i = 0; i < wordCount; i++) {
        initialiseString(firstChar);  //<-- For first iteration of for loop this will cause problem as 'firstChar' is not initialized.
        l = 0;
Vagish
  • 2,520
  • 19
  • 32
  • Thanks @Vagish, that solves my problem. May be I was too engrossed in debugging the complex constructs of the code that I forgot that error could be hidden anywhere, even in the silliest of the places. Thanks once again. – Gurucharan Sharma Feb 18 '15 at 10:12