Pig Latin converter using only null terminated c strings

I'm needing to make a pig latin converter using only null terminated cstrings. I keep getting a segmentation fault error and can not figure out why. Also, only one word is being processed from the cin.get line when it needs to be the whole sentence. Anyone have any ideas?

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;

char converter(char *, char *);

int main ()
{
const int max = 50;
char *array;
char *arrtemp;
array = new char[max];
arrtemp = new char[max];

cout << "Enter a sentence you want converted to Pig Latin: \n";
cin.getline(array, max);
cout << endl;


converter(array, arrtemp);

cout << arrtemp << " ";
delete [] arrtemp;
delete [] array;

cout << endl;

return 0;
}


char converter(char *array, char *arrtemp)
{
char first;
char ay[3] = "ay";
char way[4] = "way";
int size = 0, j = 0;
bool loop = true;

while (loop)
{
if (array[j] == ' ')
{
j++;
}
for (int i = 0; array[j] != ' '; i++)
{
arrtemp[i] = array[j];

if (array[j] == '\0' || array[j] == '.')
{
continue;
}
j++;
}
first = arrtemp[0];

//To move first letter to end of word and ad 'ay' for words not starting with vowels
if (first != 'A' && first != 'a' && first != 'E' && first != 'e' && first != 'I' && first != 'i' && first != 'O' && first != 'o' && first != 'U' && first != 'u')
{
size = strlen(arrtemp);

for (int cnt = 0; cnt == size; cnt++)
{
arrtemp[cnt] = arrtemp[cnt + 1];
}

arrtemp[size - 1] = first;
strcat(arrtemp, ay);
}

//To add way to a word starting with a vowel
else if (array[j] == '\0' || array[j] == '.')
{
loop = false;
}
else
{
strcat(arrtemp, way);
}
loop = false;
}
//cout << arrtemp << " ";

return *arrtemp;
}
I'm needing to make a pig latin converter using only null terminated cstrings.


One problems is that you aren't keeping the invariant for null terminated strings. (Yours aren't null terminated.) Feeding non-null terminated strings to strcat is a recipe for disaster.
Topic archived. No new replies allowed.