I feel so dumb for not being able to figure this out. Maybe it's because I'm sick, but for some reason, I just can't get this program to work right. Currently, it's giving me an illegal operation before it prints one line. It's probably a pointer problem, but I can't figure it out.
Anyway, the program is supposed to take in a phrase, then translate it to pig latin using the strtok function. (I'm just trying to get it to work with the phrase written into the code right now.) Also, we are to assume that the phrase has no punctuation and that all the words are at least two letters.
On with the code.
using namespace std;
char printlatinword(char* token);
char phrase="Hello world";
char* tokenptr= NULL;
tokenptr = strtok( phrase," ");
while (tokenptr != NULL)
cout<< tokenptr <<endl;
tokenptr = strtok(NULL," ");
char printlatinword(char* token)
char* firstletter = NULL;
char* translation = NULL;
firstletter = strncpy(firstletter, token, 1);
translation = strcat(token, firstletter);
translation = strcat(token, "ay\0");
The problem is definitely in the printlatinword function. When I replaced the function call with a cout<<tokenptr statement, it printed the tokens without a problem.