**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.