-
something is missing, and i just cant see it!!
Alright, heres the thing, this assignment for school is due in like 3 days, i have spent so much time staring at my code and i dont know whats wrong!! the problem is in the function, i know that for sure, something about mabey using strlan...
This is what i have so far... its almost finished, just a few small changes is all i need, and im sure its a totally obvious problem, but i only started learning C two and a half weeks ago, so were all going to place pity on the beginners, right?? haha...
//Alicia Hogue
//Chapitre 8
//Exercise 8.13
//this program takes an inputted sentance in english and
//translates it to "pig latin". Taking the first letter of each word
//place it at the end of each word in the sentance, followed
//by "ay".
#include <stdio.h>
#include <string.h>
#include <conio.h>
void PrintLatinWord(char *word);
int main(void)
{
char sentance[255];
char array1[255];
char array2[255];
char *delimiters=array1;
char *token=array2;
strcpy(delimiters, " ");
printf("Please enter a sentance to be translated:\n");
gets(sentance);
token = strtok(sentance, delimiters);
while (token != NULL)
{
PrintLatinWord(token);
printf("%s\n",token);
token = strtok(NULL, delimiters);
printf("%p\n", token);
getch();
}
return 0;
}
void PrintLatinWord(char *word)
{
int flag=0, i=0;
char temp=word[0];
char *tptr=&temp;
while(!flag)
{
word[i]=word[i+1];
//shifts every character one over to the left
if (word[i]=='\0')
flag++;
i++;
}
strcat(word,tptr);
//adds the first letter tp the end
strcat(word,"ay");
}
oh, and if you see any other errors in the code you can feel free to point them out as well... thanx!
-
Me want cookie!
Token is a pointer to the next token in the string. You can't just add some characters to this pointer (in PrintLatinWord). A better way to do this is to copy the new (Latin) word to a new buffer (lattinWord). Try something like this:
char * PrintLatinWord(char *englishWord, char *lattinWord);
int main(void)
{
char sentance[255];
char delimiters[] = " ";
char lattinWord[255];
char *token = NULL;
printf("Please enter a sentance to be translated:\n");
gets(sentance);
token = strtok(sentance, delimiters);
while (token != NULL)
{
printf("Token = [%s]\n", token);
printf("Translation = [%s]\n",PrintLatinWord(token, lattinWord));
token = strtok(NULL, delimiters);
getch();
}
return 0;
}
char * PrintLatinWord(char *englishWord, char *lattinWord)
{
strcpy(lattinWord, englishWord+1);
lattinWord[strlen(lattinWord)+1] = 0;
lattinWord[strlen(lattinWord)] = englishWord[0];
strcat(lattinWord,"ay");
return lattinWord;
}
-
Quote;
>token = strtok(sentance, delimiters);
I haven't tested this, but isn't strtok a little flakey when it comes to error handling (like, it just doesn't)
If the user were to just hit enter (i.e. not enter anything for sentence) wouldn't this pack a sad.
Maybe using something along the lines of
if ((token = strtok(sentance, delimiters))==NULL){
printf ("Sorry, invalid, please type a sentance\n");
return;
}
would be a good plan.
I could be way off base (especially with the code), but my point is I think it's probably a good idea to trap errors in strtok from the word go.
Popular pages
Recent additions