I need some assistance in getting this program to perform correctly. It's suppose to take a sentence, break it up and return each word in pig latin. Right now, the program takes a sentence such as Jump start your morning and it returns Jumpay startay youray morningay. It should return umpjay tartsay ouryay orningmay. Can anybody tell me what I am doing wrong in my DisplayPigLatin function?

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

#include <cstring>
using std::strtok;
using std::strlen;

#include <string>
using std::string;


const int LENGTH = 50;
void DisplayPigLatin( const char *);

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

	cout << "Enter a sentence to be tokenized: \n\n";
	cin.getline(sentence, LENGTH);
	cout << "\n";
	tokenPtr = strtok(sentence, " ");
	while (tokenPtr != NULL)
	{
		DisplayPigLatin(tokenPtr);
		tokenPtr = strtok(NULL, " ");
	}
	return 0;
}

void DisplayPigLatin( const char *tokenPtr )
{
	char length = strlen(tokenPtr);
	cout << tokenPtr[length];
	for (int count = 0; count < length; count++)
		cout << tokenPtr[length - (length - count)];
	cout << "ay";

}

Recommended Answers

All 3 Replies

This probably won't cause too many problems, but just for consistency's sake, use an integer:

char length = strlen(tokenPtr);

What does this line do?

cout << tokenPtr[length];

Think carefully about what you want your program to do. Write it out if you have to.

  1. Print out all the characters of the token except the first one.
  2. Print out the first character of the string.
  3. Print out 'ay'.

So, your loop should be first.

for (int count = 0; count < length; count++)
	cout << tokenPtr[length - (length - count)];

Since you want to print out all the characters of the string except the first one, count should actually start at 1. And you're being redundant with the length - (length - count) thing. If you remember your highschool math, you'll realize that the lengths cancel each other out, and count becomes positive. Thus, your loop should look like so:

for (int count = 1; count < length; count++)
   cout << tokenPtr[count];

After you've printed the loop, you need to add a line that prints out the first character of the string. You might want to do something like (char) tolower(tokenPtr[0]) in order to ensure that the character is undercase.

Finally, add a space after "ay" so that all the tokens aren't squished together.

This is what my Display function looks like now. I incorporated the changes and I get everything except the first letter. For example the word this should etur histay but instead, it returns hisay.

void DisplayPigLatin( const char *tokenPtr )
{
	int length = strlen(tokenPtr);
	
	for (int count = 1; count < length; count++)
		cout << tokenPtr[count];
	cout << "ay ";

}

How do you expect the first character of the token to print out when you have never written in your code that the first character should be printed? C++ does exactly what you tell it to do, no more. (Hint: read the last paragraph of my previous post.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.