GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #21  
Old 02-Apr-2005, 19:16
groggy23 groggy23 is offline
Junior Member
 
Join Date: Mar 2005
Posts: 38
groggy23 is on a distinguished road
Ok there's only one more problem left. Something is not storing the values for the srings right, either its being overwritten with the last two words or something is wrong with the loop. I think it might have something to do with b=0; position. However I don't want to move a minor statement with changing a lot of the code. Here is the latest code I have followed with sample output:

CPP / C++ / C Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>

char vowel(char orig[]);
char twoletter(char orig[]);
char lastrule(char orig[]);
YesNo(char check);

char newW[80];
char newV[80];
char newD[80];
char newL[80];

int main()
{
	char sentence[80];
	char piglatin[80];
	char orig[80];
	char check;

	int i=0, a=0, b=0, piglength, length;

	system("cls");				//clear screen
	printf("Please enter a sentence in all CAPS to be translated into piglatin\n");

	/* Gets sentence stored in array "sentence[]"*/
	while((sentence[i]=getche())!='\r')
	{
		i++;
	}
	sentence[i]='\0';			//adds null character at end
	length=strlen(sentence);	//# of elements stored in length

	/* Reads a word at a time and checks rule */
	while(a<=length)
	{
		orig[b]=sentence[a];
	
		if(orig[b]==' '||orig[b]=='\0')
		{
			orig[b]='\0';
			b=0;
			
			if(orig[0]=='A'||orig[0]=='E'||orig[0]=='I'||orig[0]=='O'||orig[0]=='U')
			{   
				vowel(orig);//function call for vowel rule
				strcat(piglatin, newV);
				strcat(newV, " ");
				strcpy(newW, newV);
			}//end of if

			else if(orig[0]=='T'&&orig[1]=='H'||orig[0]=='C'&&orig[1]=='H'||orig[0]=='S'&&orig[1]=='H'||orig[0]=='P'&&orig[1]=='H'||orig[0]=='W'&&orig[1]=='H'||orig[0]=='Q'&&orig[1]=='U')
			{
				twoletter(orig);  //function call for 2 letter rule
				strcat(piglatin, newD);
				strcat(newD, " ");
				strcpy(newW, newD);
			}//end of else if
    
			else
			{
				lastrule(orig);  //function call for last rule
				strcat(piglatin, newL);
				strcat(newL, " ");
				strcpy(newW, newL);
			}//end of else

		}//end of if
		else
		{
			strcpy(piglatin, newW);
			b++;
		}//end of else
		
		a++;

	}//end of while

	printf("\n\nYour translated piglatin sentence is: \n%s ", piglatin); 
	printf("\n\nWould you like to enter another sentence to be translated(Y/N)?:  ");
	scanf(" %c", &check);
	YesNo(check);

}//end of main




char vowel(char orig[])
{
    strcat(orig,"AY");
	strcpy(newV, orig);

return(newV);

}//end of function



char twoletter(char orig[])
{

char first, append[]="H";

first=orig[0];

if(orig[1]=='H')
{
	orig+=2;      //point at 3rd char
	append[0] = first;  //add on the starting letter
    strcat(orig, append);
    strcat(orig,"HAY");  //and then the 'H' and "AY"
}//end of IF

if(orig[1]=='U')
{
    orig+=2;
    append[0] = first;
    strcat(orig, append);
    strcat(orig, "UAY");
}//end of IF

strcpy(newD, orig);

return(newD);
}//end of function



char lastrule(char orig[])
{

char first;
char append[]="H";

first=orig[0];    //gets first letter of word

orig++;	
append[0] = first;
strcat(orig,append);
strcat(orig,"AY");
strcpy(newL, orig);

return(newL);
}//end of function



YesNo(char check)
{

if(check=='Y')
{
	main();
}//end of if

}//end of function

OUTPUT:

Please enter a sentence in all CAPS to be translated into piglatin
ALL YOUR BASE ARE BELONG TO US

Your translated piglatin sentence is:
OTAY USAY

Would you like to enter another sentence to be translated(Y/N)?:
  #22  
Old 03-Apr-2005, 00:26
nkhambal nkhambal is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
First of all get your input part correct. Did you notice that you are loosing the last character of the sentence? Following statement
CPP / C++ / C Code:
sentence[i]='\0';

will replace last character in your sentence to null. It should be
CPP / C++ / C Code:
sentence[i+1]='\0';

I am posting my full program. I don't have 3 functions like you, but just one which does the similar job. See if it can help.

CPP / C++ / C Code:
#include <stdio.h>
#include <ctype.h>

char * piglatin(char *);

int main(int argc, char *argv[])
{
	char word[50];
	char * temp;
	char tword[50];
	int len,a,b,i;
	
	printf("Enter the name: ");
	i=0;
	a=b=0;
	while((word[i]=getchar())!='\n')
	{
		i++;
	}
	word[i+1] = '\0';
	
	len=strlen(word);
	
	while (a<len)
	{
		if (isspace(word[a]) || a == len-1)
		{
			tword[b]='\0';
			b=0;
			a++;
			printf("%s ",piglatin(tword));			
		} else {
			tword[b]=word[a];
			a++;
			b++;
		}
	} 
	
	return 0;
}

char * piglatin(char *word)
{
	static char temp[50];

	memset(temp,'\0',50);

	if (toupper(word[0]) == 'A' || toupper(word[0]) == 'E' || toupper(word[0]) == 'I' || toupper(word[0]) == 'O' || toupper(word[0]) == 'U' )
	{
		strcpy(temp,++word);		  //copy word starting from second character to temp
		temp[strlen(temp)]=*(--word); //copy the first char of word to end of temp
		temp[strlen(temp)+1]= '\0';   // Null terminate temp
		strcat(temp,"way");			  // append "way" to the string.
		return temp;
	} else {
		strcpy(temp,++word);			//copy word starting from second character to temp
		temp[strlen(temp)]=*(--word);	//copy the first char of word to end of temp
		temp[strlen(temp)+1]= '\0';		// Null terminate temp
		strcat(temp,"ay");				// append "ay" to the string.
		return temp;
	}
}

  #23  
Old 03-Apr-2005, 17:58
groggy23 groggy23 is offline
Junior Member
 
Join Date: Mar 2005
Posts: 38
groggy23 is on a distinguished road
Thanks for you help again nkhambal, and other that have contributed to this thead. I apologize for not stating the rules as the professor wants this program done. I fixed my problem, I was making it too complicated by adding the strcpy and strcat commands in the loop itself. I found out that strcpy was getting replaced by the next word and so forth until it only take the last 2 words of the sentence. I just took those statements out and replaced it with printf statements after each rule. Everything works great now. Once again, thanks for all the help.

I just have one more question about a warning I get about the returning string. You already have my code as stated in this post many times, here are the warnings I get and this is for all 3 functions that is returning the string.

c:\program files\microsoft visual studio\myprojects\piglatin\piglatin.c(103) : warning C4047: 'return' : 'char ' differs in levels of indirection from 'char [32]'
c:\program files\microsoft visual studio\myprojects\piglatin\piglatin.c(136) : warning C4047: 'return' : 'char ' differs in levels of indirection from 'char [32]'
c:\program files\microsoft visual studio\myprojects\piglatin\piglatin.c(15 : warning C4047: 'return' : 'char ' differs in levels of indirection from 'char [32]'
  #24  
Old 03-Apr-2005, 19:05
nkhambal nkhambal is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
The Answer to the questions you asked is in your question itself

This is your function prototype

CPP / C++ / C Code:
char vowel(char orig[]);
char twoletter(char orig[]);
char lastrule(char orig[]);

have a close look at these prototypes and your function definition as well. They are supposed to return a string right? Are they really ?

Moreover, I mentioned in one of my previous post that, although you are returning the strings to main(), nothing is being done in main() to capture these values (strings). They are behaving as if they are of void return type. This can affect your grades seriously, if your professor is picky about these things. You are operating on global array variables. Not so good idea. You can pass the array to functions to modify them inside each functions. Since arrays are always passed by reference, your arrays will be consitant between main() and each function. Pass a single array to all three functions which will hold the converted string and print it in main().

Good luck
  #25  
Old 03-Apr-2006, 18:54
malibu malibu is offline
New Member
 
Join Date: Apr 2006
Posts: 8
malibu is on a distinguished road

Re: piglatin program


Mine assignment is a very similar one and i couldnt use the second rule which i think shoud be working and i could not understand how you made it word for the second, third and more words. my code only works for the first word. here it is:
------------------------------------------------------------------------
CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define SIZE 80
#define SIZE2 150

char Menu();
void Rules_Chooser(char myinput[ ], char myoutput[]);//Rule Function
void First_Rule(char myinput[ ], char myoutput[]);//Two Letter Rule Function
void Second_Rule(char myinput[ ], char myoutput[]);//Vowel Rule Function
void Third_Rule(char myinput[ ], char myoutput[]);//Last Rule Function


int main()
{
	Menu();//Calls Input and cancel menu

return 0;
}

char Menu()
{
	char myinput[SIZE];
	char myoutput[SIZE2];
	printf("Please enter the phrase to translate(enter quit or q to quit):");
		gets(myinput);//gets input

		if(myinput[0]=='q')//Checks if the user wants to quit
		{
		printf("Thanks for using the translator\n");
		}
		else//if not then calls rules function for the input
		{
			Rules_Chooser(myinput,myoutput); //Rules Function
			Menu();//For the second time when user wants to enter new input
		}

	return 0;

}

void Rules_Chooser(char myinput[ ],char myoutput[ ])//Rules
{
	int length;
	length=strlen(myinput);//How long is the input?
	myoutput[0]="H";

	//Check for the first rule
	if((!_strnicmp(myinput,"TH",2))||(!_strnicmp(myinput,"CH",2))||(!_strnicmp(myinput,"SH",2))||(!_strnicmp(myinput,"PH",2))||(!_strnicmp(myinput,"WH",2))||(!_strnicmp(myinput,"QU",2)))
	{
		First_Rule(myinput,myoutput);
	}//check for the second rule
	else if((!_strnicmp(myinput,"A",1))||(!_strnicmp(myinput,"E",1))||(!_strnicmp(myinput,"I",1))||(!_strnicmp(myinput,"O",1))||(!_strnicmp(myinput,"U",1)))
	{
		Second_Rule(myinput,myoutput);
	}
	else//if neither than call third rule
	{
		Third_Rule(myinput,myoutput);
	}


}
//First Rule Function(two letter rule)
void First_Rule(char myinput[ ],char myoutput[ ])
{
		char temp;
		temp=myinput[0];
		myoutput[0]=temp;


	if((myinput[1]=='H')||(myinput[1]='h'))
	{
		myinput+=2;//Point to the third character
		strcat(myinput,myoutput);//copy the first character to the end
		strcat(myinput,"HAY");//Add the HAY at the end of all
		printf("%s\n",myinput);//Give Result
	}
	else if((myinput[1]=='U')||(myinput[1]='u'))
	{
		myinput+=2;//Point to the third character
		strcat(myinput,myoutput);//copy the first character to the end
		strcat(myinput,"UAY");//Add the HAY at the end of all
		printf("%s\n",myinput);//Give Result
	}

}

//Second rule(vowel rule)
void Second_Rule(char myinput[ ],char myoutput[ ])
{
	strcat(myinput,"AY");
	printf("%s\n",myinput);

}
//Third(last) rule
void Third_Rule(char myinput[ ],char myoutput[ ])
{
	printf("3rd rule\n");
}
  #26  
Old 03-Apr-2006, 19:59
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: piglatin program


Quote:
Originally Posted by malibu
Mine assignment is a very similar one and i couldnt use the second rule which i think shoud be working and i could not understand how you made it word for the second, third and more words. my code only works for the first word.
Actually, it seems to be written for only one word, not a sentence. On top of that, I get multiple warnings which probably contribute to whatever is wrong. I alse get an error on compile -- which you probably don't I would guess.

It might behoove you to read the Guidelines so you can properly ask questions that we can answer. You give too little information so we have to pour through your code trying to figure out what it does just to figure out the mysterious way it doesn't work. You should tell us. Guideline #1, #2.

You also use gets() which is a very dangerous function. Read this for an explanation of why.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #27  
Old 03-Apr-2006, 20:22
malibu malibu is offline
New Member
 
Join Date: Apr 2006
Posts: 8
malibu is on a distinguished road

Re: piglatin program


Actually the purpose is code was explained in pervious questions and it is the same think nevermind.

I know gets is not a good function but how am i supposed to use fgets. I think it is to write to a file not just to get it from the user. My problem is that i couldnt make the translation for the first rule. It takes the TH,Wh, etc letters from the beginning of the words and add it to the end of the words and AY follows it. and another thing is it has to do it for every word that is entered not just one word. I think i have to use a while loop which gets a new word every time but i could not understand how i am supposed to do that too.

I dont get any warnings by the way.
  #28  
Old 04-Apr-2006, 00:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: piglatin program


Quote:
Originally Posted by malibu
I know gets is not a good function but how am i supposed to use fgets. I think it is to write to a file not just to get it from the user.
Read the entire link about gets()...


Quote:
Originally Posted by malibu
My problem is that i couldnt make the translation for the first rule. It takes the TH,Wh, etc letters from the beginning of the words and add it to the end of the words and AY follows it.
Top priority your test for the first rule:
CPP / C++ / C Code:
if((!_strnicmp(myinput,"TH",2))||(!_strnicmp(myinput,"CH",2))||(!_strnicmp
(myinput,"SH",2))||(!_strnicmp(myinput,"PH",2))||(!_strnicmp(myinput,"WH"
,2))||(!_strnicmp(myinput,"QU",2)))
1) needs to be formatted
2) needs to be rethought

First: 187 character lines are a bad thing. Make it readable with whitespace:
CPP / C++ / C Code:
if ((!_strnicmp(myinput, "TH", 2)) ||
    (!_strnicmp(myinput, "CH", 2)) ||
    (!_strnicmp(myinput, "SH", 2)) ||
    (!_strnicmp(myinput, "PH", 2)) ||
    (!_strnicmp(myinput, "WH", 2)) ||
    (!_strnicmp(myinput, "QU", 2)))
This goes for the entire program. It's hard to read without it. Check out this info for possible formatting options.

Second: What about GHOST, KNIFE, PTOMAIN, etc. You should probably test to see if the first and second characters are not vowels instead. Don't list all the letter pairs you can think of. Simply test each character.

Now, for the rule itself:
CPP / C++ / C Code:
char temp;
temp=myinput[0];  // why not just myoutput[0] = myinput[0]?
myoutput[0]=temp;
Also, I thought you had to move the 1st and 2nd characters to the end. This moves the 1st character to the 1st position in your output.
Then:
CPP / C++ / C Code:
if((myinput[1]=='H')||(myinput[1]='h'))
Again, why? Didn't you establish that the 2nd character must also move? No need for a test.

Let's just run through your test with the word "CHAIR":
CPP / C++ / C Code:
void First_Rule(char myinput[ ],char myoutput[ ])
{                       // myinput="CHAIR"  myoutput="Hxxxxxxx"
                        // the 'xxxxxxxx' are garbage characters because 
                        // when you created the variable garbage is in
                        // the buffer's data
    char temp;
    temp=myinput[0];
    myoutput[0]=temp;   // myinput="CHAIR"  myoutput="Cxxxxxxx"


    if((myinput[1]=='H')||(myinput[1]='h'))  // this is true...
    {
        myinput+=2;                 // myinput="AIR"  myoutput="Cxxxxxxx"
        strcat(myinput,myoutput);   // myinput="AIRCxxxxxxx"  myoutput="Cxxxxxxx"
        strcat(myinput,"HAY");      // myinput="AIRCxxxxxxxHAY"  myoutput="Cxxxxxxx"
        printf("%s\n",myinput);     
    }
}
But myinput in your mainline actually holds "CHAIRCxxxxxxxHAY"
Shouldn't you move the characters into myoutput?

Check this out -- you already know you need to move 2 characters and fix it so your input word ends in \0, so:
CPP / C++ / C Code:
//First Rule Function(two letter rule)
void First_Rule(char *myinput, char *myoutput)
{
    int ln;
    strcpy(myinput+2,myoutput);  // copy the 3rd char to end
    ln = strlen(myoutput);       // get length of the output
    myoutput[ln] = myinput[0];   // move first char
    myoutput[ln+1] = myinput[1]; // move second char
    myoutput[ln+2] = 'A';        // piglatinize the word
    myoutput[ln+3] = 'Y';        // 
    myoutput[ln+4] = '\0';       // end with null terminator
   
    printf("%s\n",myoutput);     
}

Now you can rework your other rules accordingly.


Quote:
Originally Posted by malibu
and another thing is it has to do it for every word that is entered not just one word. I think i have to use a while loop which gets a new word every time but i could not understand how i am supposed to do that too.
Then you have to look at you input and move each word one at a time to another buffer so you can work on it. Then when done with that word, move the next.

This requires you to look at the input buffer a character at a time and move each character that's part of a word to the work buffer and skip over the characters that aren't part of a word -- like SPACE.

Quote:
Originally Posted by malibu
I dont get any warnings by the way.
Ahhh, different compilers. Yours allows syntax that my compiler claims is questionable.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #29  
Old 04-Apr-2006, 10:25
malibu malibu is offline
New Member
 
Join Date: Apr 2006
Posts: 8
malibu is on a distinguished road

Re: piglatin program


I did the if statements like you said and thanks they look way to easier to read. i didnt now i oculd do it like that at C even though i use it at ASP
and i made it work with the fgets() with this line.
CPP / C++ / C Code:
		
fgets(myinput,80,stdin);//=='\n');//gets input
if (myinput[strlen(myinput)-1] == '\n') myinput[strlen(myinput)-1] = '\0';

There is a problem with this code. It stops working at line where it copies the 3rd char. to end.
and can i use the strings like that > *myinput at function or is it supposed to be myinput[ ]?
CPP / C++ / C Code:
void First_Rule(char *myinput, char *myoutput)
{
	    int ln;

    strcpy(myinput+2,myoutput);  // copy the 3rd char to end <<< It stops here
    ln = strlen(myoutput);       // get length of the output
    myoutput[ln] = myinput[0];   // move first char
    myoutput[ln+1] = myinput[1]; // move second char
    myoutput[ln+2] = 'A';        // piglatinize the word
    myoutput[ln+3] = 'Y';        // 
    myoutput[ln+4] = '\0';       // end with null terminator
   
    printf("%s\n",myoutput);     
}

and i got rid of the warning now. i just have three and they are because of the strcpy functions and i think it is because it is an old function.

Thanks for your help.

Can i also check each word by this loop
CPP / C++ / C Code:
        length=strlen(myinput); 
	while(a<length)
	{
		if(myinput[b]==' '||myinput[b]=='\0')
		{
			myinput[b]='\0';
			b=0;
		}//end of if
		else
		{
			b++;
		}//end of else
		a++;
	}
  #30  
Old 04-Apr-2006, 12:04
malibu malibu is offline
New Member
 
Join Date: Apr 2006
Posts: 8
malibu is on a distinguished road

Re: piglatin program


oh sorry it turned out i was using the strcpy wrong.
it should have been like
CPP / C++ / C Code:
 strcpy(myoutput,myinput+2);
not like
CPP / C++ / C Code:
 strcpy(myinput+2,myoutput);
so thanks for the helps but i still cant do it for every word. just the first word.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 20:06

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:17.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.