|
 |
|
 |
05-22-2005, 12:29 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 9
|
hey redhead quick question about c/c++
I am making a pig latin code. I have most of it worked out but getting it to take off constantsand then moving them to the end of the word before adding ay is giving me alot of trouble. this is what i have so far.
Quote:
/*Pig Latin*/
/*Valerie*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int x=0;
int y=0;
int wordcount=0;
char fword[80];
char oneword[80];
void translate (void);
int main()
{
printf("\n\tWelcome to Pig Latin.Please enter a sentence you would like to tranlate\n");
fgets(fword,81, stdin);
printf("\n%s\n",fword);
while (fword[x]!='\0')
{
while (fword[x]!= ' ' && fword[x] != '.')
{
printf("%c",fword[x]);
oneword[y]=fword[x];
++y;
++x;
}
if (fword[x] == '.')
{
oneword[y]='\0';
translate();
break;
}
else
{
printf("\n");
oneword[y]='\0';
translate();
++x;
y = 0;
}
}
getch();
}
void translate (void)
{
++wordcount;
printf("\n I will translate %s \n ",oneword);
char oneword;
int temp;
int v;
{
while (oneword !='a' && oneword !='e' && oneword !='i' && oneword !='o' && oneword !='u'&& oneword !='y')
--oneword;
++v;
v=temp;
printf("\n\t%c %iay",oneword,temp);
printf("\n\n\n\t The number of words is %i",wordcount);
}
}
|
Sorry i forgot to add my question. how do i takje a word entered. and then take off the constants and move them to the end of the word. i thought this was correct but its not.
Thanks
~Valerie
Last edited by rogue; 05-22-2005 at 02:08 PM.
Reason: to make sure i have this the way valmont wants it.
|
|
|
05-22-2005, 02:13 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
|
You've got some errors in your pig latin algorithm, I've made some changes to the program, I hope the comments will explain why those things are made.
Code:
/*Pig Latin*/
/*Valerie*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LENGTH 80
#define ADDITION "ay"
int translate(char* trans);
int main()
{
int i = 0, j = 0, wordcount = 0;
char line[LINE_LENGTH +1];
char word[LINE_LENGTH +strlen(ADDITION)]; /* make room for ay at the end */
printf("\n\tWelcome to Pig Latin.\n");
printf("\tPlease enter a sentence you would like to translate\n");
printf("Real line:\t");
fflush(stdout);
fgets(line, LINE_LENGTH, stdin);
line[strlen(line) -1] = '\0'; /* remove '\n' at the end */
printf("In pig Latin:\t");
while (line[i]!='\0')
{
/* pick out a single word in the line */
while (line[i]!= ' ' && line[i] != '.' && line[i] != '\0')
word[j++] = line[i++];
++wordcount;
word[j] = '\0';
translate(word);
j = 0;
if(line[i] == '.')
break;
++i;
/* make sure extra spaces aren't screwing it up for us */
while(line[i] == ' '|| line[i] == '\t' || line[i] == '\r')
++i;
}
printf("\nNumber of words translated: %d\n", wordcount);
printf("\nPress enter to exit\n");
getchar();
return 0;
}
int translate (char* trans)
{
int i = 0, j = 0;
char temp1[LINE_LENGTH +1];
char temp2[LINE_LENGTH +strlen(ADDITION)];
temp1[0] = '\0';
temp2[0] = '\0';
/* pick out the consonants befor any vowel */
while(trans[i] !='a' && trans[i] !='e' && trans[i] !='i'
&& trans[i] !='o' && trans[i] !='u'&& trans[i] !='y')
temp1[i] = trans[i++];
temp1[i] = '\0';
/* copy the rest of the word */
while(trans[i] != '\0')
temp2[j++] = trans[i++];
temp2[j] = '\0';
/* append the consonants to the word */
strncat(temp2, temp1, strlen(temp1));
/* append the ay to the end of the word */
strncat(temp2, ADDITION, strlen(ADDITION));
printf("%s ", temp2);
return 0;
}
Now there are severa things that can be further developed on this, the translate() could be made to take the entire line, and translate one word at a time, then return with the translated part... But then again I'm not sure, where you want to use this.
|
|
|
05-23-2005, 01:22 AM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
|
Here is a version, where I've implemented the translate function to take the entire line and convert everything in it, befor returning.
How ever, this introduces some pitfalls, if you have a line of exact 80 chars, and it consist of several words, then this version will have some unexplainable outcome.
This can be solved by using a line like:
Code:
temp2 = (char*) realloc(temp2, (LINE_LENGTH + (wordcount+1)*strlen(ADDITION))*sizeof(char));
where the value k is reset to the length of the temp2 variable in translate(), and remember to free() it, once we're done.
But since this is just for fun, I dont wanna get into using those things.
Another feature it implements, it will accept vowels that are capital letters, which the privius one didn't.
Code:
/*Pig Latin*/
/*Valerie*/
#include <stdio.h>
#include <string.h>
#define LINE_LENGTH 80
#define ADDITION "ay"
int translate(char *trans);
int main()
{
int i = 0, j = 0, wordcount = 0;
char line[LINE_LENGTH +strlen(ADDITION)];
printf("\n\tWelcome to Pig Latin.\n");
printf("\tPlease enter a sentence you would like to translate\n");
printf("Real line:\t");
fflush(stdout);
fgets(line, LINE_LENGTH, stdin);
line[strlen(line) -1] = '\0'; /* remove '\n' at the end */
wordcount = translate(&line);
printf("In pig Latin:\t%s", line);
printf("\nNumber of words translated: %d\n", wordcount);
printf("\nPress enter to exit\n");
getchar();
return 0;
}
int translate(char *trans)
{
char word[LINE_LENGTH +strlen(ADDITION)];
char temp1[LINE_LENGTH +strlen(ADDITION)];
char temp2[LINE_LENGTH +strlen(ADDITION)];
int i = 0, j = 0, k = 0, wordcount = 0;
while (trans[i]!='\0')
{
/* pick out a single word in the line */
while (trans[i]!= ' ' && trans[i] != '.' && trans[i] != '\0')
word[j++] = trans[i++];
++wordcount;
word[j] = '\0';
j = 0;
{ /* pick out the consonants befor any vowel */
while(word[j] !='a' && word[j] !='e' && word[j] !='i'
&& word[j] !='o' && word[j] !='u'&& word[j] !='y'
&& word[j] !='A' && word[j] !='E' && word[j] !='I'
&& word[j] !='O' && word[j] !='U'&& word[j] !='Y')
temp1[j] = word[j++];
temp1[j] = '\0';
/* copy the rest of the word */
while(word[j] != '\0')
temp2[k++] = word[j++];
temp2[k] = '\0';
/* append the consonants to the word */
strncat(temp2, temp1, strlen(temp1));
/* append the ay to the end of the word */
strncat(temp2, ADDITION, strlen(ADDITION));
}
/* since we're dealing with the entire line,
* we let temp2 expand and end up holding every
* word found in the line
*/
k = strlen(temp2);
temp2[k++] = ' ';
temp2[k] = '\0';
j = 0;
if(trans[i] == '.')
break;
++i;
/* make sure extra spaces aren't screwing it up for us */
while(trans[i] == ' '|| trans[i] == '\t' || trans[i] == '\r')
++i;
}
memcpy(trans, temp2, strlen(temp2));
trans[strlen(temp2)] = '\0';
return wordcount;
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:55 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|