Pig Latin - String Manipulation

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4688 Views - Last Post: 11 February 2007 - 12:37 PM Rate Topic: -----

#1 MaStA819  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 07

Pig Latin - String Manipulation

Posted 10 February 2007 - 06:23 PM

I have to build a pig latin converter. It isn't an in depth one that needs to care about vowels, punctuation, etc. All it has to do is simply receive a sentence, break up the words, move the first character of each word to the end, and add ay. I've been messing with this for quite some time and the instructor gave us the first part of his program so I am trying to work from that. Below is currently what I have. Currently the program will simply break the sentence up into words and print the individual words on separate lines. I need those words to be converted to pig latin prior to printing. The cout will soon call to the printLatinWord function once I can figure out how to move characters around. I do know that a void function cannot return a value. I was just messing around with some things and that is how it is currently commented out. Everything from cout << "Enter a sentence:\n"; and up was provided from the teacher as a starter so I know how he has some basic things setup. Hopefully it doesn't look like I'm asking you all to do anything for me, but please point me in the right direction as to how to move characters around. Thank you!

BTW I'm using Windows Vista and Visual C++ 2005.

Jared

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <cstring>
using std::strtok;

const int SIZE = 80;

// function prototype
void printLatinWord( char const * const );

int main()
{
char sentence[ SIZE ];
char *tokenPtr;

cout << "Enter a sentence:\n";
cin.getline(sentence, SIZE);

tokenPtr = strtok(sentence, " ");

while (tokenPtr != NULL)
{
	cout << tokenPtr << '\n';
	tokenPtr = strtok(NULL, " ");
}
	
	return 0;
}//End main function

/**
void printLatinWord(char const *tokenPtr)
{
	String result;
	if(tokenPtr.length() > 0)
		result = tokenPtr.sub(1, tokenPtr.length() - 1) +tokenPtr[0] + "ay";		
	return result;

}//End printLatinWord function
**/



Is This A Good Question/Topic? 0
  • +

Replies To: Pig Latin - String Manipulation

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Pig Latin - String Manipulation

Posted 10 February 2007 - 08:10 PM

You are using char pointers to hold the strings...is that by direction for the assignment? I'm going to assume so, which means that you'll likely be using the string manipulation functions from the cstring header.

Before going too much further, are you familiar with common array manipulation (looping through the elements, etc...)?
Was This Post Helpful? 0
  • +
  • -

#3 MaStA819  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 07

Re: Pig Latin - String Manipulation

Posted 10 February 2007 - 08:46 PM

View PostAmadeus, on 10 Feb, 2007 - 08:10 PM, said:

You are using char pointers to hold the strings...is that by direction for the assignment? I'm going to assume so, which means that you'll likely be using the string manipulation functions from the cstring header.

Before going too much further, are you familiar with common array manipulation (looping through the elements, etc...)?


Can i use the string functions that are currently in the printLatinWord function with characters then? I tried and I got an error saying that there wasn't a class or something. I defined everything inside a class and the errors went away, but then I got some weird error about unexpected end of file. I had to rearrange a few things like putting main as the last function and also taking the prototype for the printLatinWord function out. I am decent I think with working with array's. Are you saying using like for loops with the array?

I don't have to use char pointers. I was just trying to use the little bit of code the instructor provided for a start on the program. We do not have to follow that though.

Thank you,
Jared
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Pig Latin - String Manipulation

Posted 10 February 2007 - 09:27 PM

To properly answer your questions, I'd have to take a look at the other code you've written, as well as see the error messages you received. In your original function, you'd attempted to use things like the .length() property of the string object, but were performing it on a char pointer. Char pointer do not have a .length() property - in those cases, you would use the strlen() function.

Of course, if you were using string objects, you'd have access to all the methods of that class.
Was This Post Helpful? 0
  • +
  • -

#5 MaStA819  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 07

Re: Pig Latin - String Manipulation

Posted 10 February 2007 - 09:56 PM

View PostAmadeus, on 10 Feb, 2007 - 09:27 PM, said:

To properly answer your questions, I'd have to take a look at the other code you've written, as well as see the error messages you received. In your original function, you'd attempted to use things like the .length() property of the string object, but were performing it on a char pointer. Char pointer do not have a .length() property - in those cases, you would use the strlen() function.

Of course, if you were using string objects, you'd have access to all the methods of that class.


Take this code for example. When I compile it I get this one and only error.

piglatintest.cpp(45) : fatal error C1004: unexpected end-of-file found

Thank you,
Jared

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <cstring>
using std::strtok;

#include <string>
using std::string;

const int SIZE = 80;

class PigLatin
{

	void printLatinWord(char const *tokenPtr)
	{
		string result;
		if(tokenPtr.length() > 0)
			result = tokenPtr.sub(1, tokenPtr.length() - 1) +tokenPtr[0] + "ay";

	}//End printLatinWord function

	int main()
	{
	char sentence[ SIZE ];
	char *tokenPtr;

	cout << "Enter a sentence:\n";
	cin.getline(sentence, SIZE);

	tokenPtr = strtok(sentence, " ");

	while (tokenPtr != NULL)
	{
		cout << printLatinWord(char const *tokenPtr) << '\n';
		tokenPtr = strtok(NULL, " ");
	}
		
		return 0;
	}//End main function

}//End PigLatin class


Was This Post Helpful? 0
  • +
  • -

#6 MaStA819  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 07

Re: Pig Latin - String Manipulation

Posted 11 February 2007 - 11:46 AM

Any ideas? This thing along with another project is due midnight tonight and I'm getting a bit nervous. I've been working these two projects for several days now so I'm trying...

Thanks,
Jared
Was This Post Helpful? 0
  • +
  • -

#7 YettaMaye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-February 07

Re: Pig Latin - String Manipulation

Posted 11 February 2007 - 11:56 AM

View PostMaStA819, on 11 Feb, 2007 - 11:46 AM, said:

Any ideas? This thing along with another project is due midnight tonight and I'm getting a bit nervous. I've been working these two projects for several days now so I'm trying...

Thanks,
Jared



hey this is yetta, i had an idea-but i need your help, you still here?
Was This Post Helpful? 0
  • +
  • -

#8 MaStA819  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 07

Re: Pig Latin - String Manipulation

Posted 11 February 2007 - 11:58 AM

View PostYettaMaye, on 11 Feb, 2007 - 11:56 AM, said:

View PostMaStA819, on 11 Feb, 2007 - 11:46 AM, said:

Any ideas? This thing along with another project is due midnight tonight and I'm getting a bit nervous. I've been working these two projects for several days now so I'm trying...

Thanks,
Jared



hey this is yetta, i had an idea-but i need your help, you still here?


What's up?

Jared
Was This Post Helpful? 0
  • +
  • -

#9 YettaMaye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-February 07

Re: Pig Latin - String Manipulation

Posted 11 February 2007 - 11:58 AM

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <cstring>
using std::strtok;

#include <string>
using std::string;

const int SIZE = 80;

void printLatinWord(char const *tokenPtr)
{
string result;
if(tokenPtr.length() > 0)
result = tokenPtr.sub(1, tokenPtr.length() - 1) +tokenPtr[0] + "ay";

}//End printLatinWord function

int main()
{
char sentence[ SIZE ];
char *tokenPtr;

cout << "Enter a sentence:\n";
cin.getline(sentence, SIZE);

tokenPtr = strtok(sentence, " ");

while (tokenPtr != NULL)
{
cout << printLatinWord(char const *tokenPtr) << '\n';
tokenPtr = strtok(NULL, " ");
}

return 0;
}//End main function
Was This Post Helpful? 0
  • +
  • -

#10 MaStA819  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 07

Re: Pig Latin - String Manipulation

Posted 11 February 2007 - 12:09 PM

View PostYettaMaye, on 11 Feb, 2007 - 11:58 AM, said:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <cstring>
using std::strtok;

#include <string>
using std::string;

const int SIZE = 80;

void printLatinWord(char const *tokenPtr)
{
string result;
if(tokenPtr.length() > 0)
result = tokenPtr.sub(1, tokenPtr.length() - 1) +tokenPtr[0] + "ay";

}//End printLatinWord function

int main()
{
char sentence[ SIZE ];
char *tokenPtr;

cout << "Enter a sentence:\n";
cin.getline(sentence, SIZE);

tokenPtr = strtok(sentence, " ");

while (tokenPtr != NULL)
{
cout << printLatinWord(char const *tokenPtr) << '\n';
tokenPtr = strtok(NULL, " ");
}

return 0;
}//End main function


Did you try compiling that prior to posting it? I get like 6 errors.

Jared
Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Pig Latin - String Manipulation

Posted 11 February 2007 - 12:10 PM

I'd suggest something like the following:
void printLatinWord(char *tokenPtr)
{
   string s1(tokenPtr);
   string s2 = s1.substr(1)+s1[0]+"ay";
   cout<<s2<<endl;
}//End printLatinWord function


It converts the char pointer to a string object, then assigns the pieces required to another string.

The errors in the previous pots occur because the user is attempting to use methods belonging to the string object to a char pointer, or c style string. This cannot be done without conversion.
Was This Post Helpful? 0
  • +
  • -

#12 MaStA819  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 07

Re: Pig Latin - String Manipulation

Posted 11 February 2007 - 12:12 PM

View PostAmadeus, on 11 Feb, 2007 - 12:10 PM, said:

I'd suggest something like the following:
void printLatinWord(char *tokenPtr)
{
   string s1(tokenPtr);
   string s2 = s1.substr(1)+s1[0]+"ay";
   cout<<s2<<endl;
}//End printLatinWord function


It converts the char pointer to a string object, then assigns the pieces required to another string.

The errors in the previous pots occur because the user is attempting to use methods belonging to the string object to a char pointer, or c style string. This cannot be done without conversion.


Great, but I still get that strange fatal error C1004: unexpected end-of-file found

Any clue why this is?

Thanks,
Jared
Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Pig Latin - String Manipulation

Posted 11 February 2007 - 12:15 PM

Try posting the following into a new file:
#include <iostream>
#include<string>
#include <cstring>
using namespace std;

const int SIZE = 80;

// function prototype
void printLatinWord(char *tokenPtr);

int main()
{
char sentence[ SIZE ];
char *tokenPtr;

cout << "Enter a sentence:\n";
cin.getline(sentence, SIZE);

tokenPtr = strtok(sentence, " ");

while (tokenPtr != NULL)
{
	printLatinWord(tokenPtr);
	tokenPtr = strtok(NULL, " ");
}
	return 0;
}//End main function


void printLatinWord(char *tokenPtr)
{
   string s1(tokenPtr);
   string s2 = s1.substr(1)+s1[0]+"ay";
   cout<<s2<<endl;
}//End printLatinWord function


Or alternatively, please post the entire piece of code you are using, and we can try and find the reason from there.
Was This Post Helpful? 0
  • +
  • -

#14 MaStA819  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-February 07

Re: Pig Latin - String Manipulation

Posted 11 February 2007 - 12:17 PM

View PostAmadeus, on 11 Feb, 2007 - 12:15 PM, said:

Try posting the following into a new file:
#include <iostream>
#include<string>
#include <cstring>
using namespace std;

const int SIZE = 80;

// function prototype
void printLatinWord(char *tokenPtr);

int main()
{
char sentence[ SIZE ];
char *tokenPtr;

cout << "Enter a sentence:\n";
cin.getline(sentence, SIZE);

tokenPtr = strtok(sentence, " ");

while (tokenPtr != NULL)
{
	printLatinWord(tokenPtr);
	tokenPtr = strtok(NULL, " ");
}
	return 0;
}//End main function


void printLatinWord(char *tokenPtr)
{
   string s1(tokenPtr);
   string s2 = s1.substr(1)+s1[0]+"ay";
   cout<<s2<<endl;
}//End printLatinWord function


Or alternatively, please post the entire piece of code you are using, and we can try and find the reason from there.


Sir, I believe you made a winner! That one works great, but I will post what I had just for finding out what was wrong with it.

Thank you very very much!
Jared

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <cstring>
using std::strtok;

#include <string>
using std::string;

const int SIZE = 80;

class PigLatin
{

	void printLatinWord(char *tokenPtr)
	{
	   string s1(tokenPtr);
	   string s2 = s1.substr(1) + s1[0] + "ay";
	   cout << s2 << endl;
	}//End printLatinWord function

	int main()
	{
	char sentence[ SIZE ];
	char *tokenPtr;

	cout << "Enter a sentence:\n";
	cin.getline(sentence, SIZE);

	tokenPtr = strtok(sentence, " ");

	while (tokenPtr != NULL)
	{
		cout << printLatinWord(char const *tokenPtr) << '\n';
		tokenPtr = strtok(NULL, " ");
	}//End while statement
		
	return 0;
	}//End main function

}//End PigLatin class


Was This Post Helpful? 0
  • +
  • -

#15 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Pig Latin - String Manipulation

Posted 11 February 2007 - 12:19 PM

In your code, the entire set is wrapped up in a class. As such, the compiler does not see the main function as a program entry point.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2