views:

410

answers:

5

Hi,

This looks like homework stuff but please be assured that it isn't homework. Just an exercise in the book we use in our c++ course, I'm trying to read ahead on pointers..

The exercise in the book tells me to split a sentence into tokens and then convert each of them into pig latin then display them..

pig latin here is basically like this: ball becomes allboy in piglatin.. boy becomes oybay.. take the first letter out, put it at the end then add "ay"..

so far this is what i have:

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

#include <cstring>
using std::strtok;
using std::strcat;
using std::strcpy;

void printPigLatin( char * );

int main()
{
    char sentence[500];
    char *token;

    cout << "Enter string to tokenize and convert: "; 
    cin.getline( sentence, 500 );

    token = strtok( sentence, " " );

    cout << "\nPig latin for each token will be: " << endl;

    while( token != NULL ) 
    {
     printPigLatin( token );
     token = strtok( NULL, " " );
    }

    return 0;
}

void printPigLatin( char *word )
{
    char temp[50];

    for( int i = 0; *word != '\0';  i++ )
    {
     temp[i] = word[i + 1];
    }

    strcat( temp, "ay" );
    cout << temp << endl;
}

I understand the tokenizing part quite clearly but I'm not sure how to do the pig latin.. i tried to start by simply adding "ay" to the token and see what the results will be .. not sure why the program goes into an infinite loop and keeps on displaying "ayay" .. any tips?

EDIT: this one works fine now but im not sure how to add the first letter of the token before adding the "ay"

EDIT: this is how i "see" it done but not sure how to correctly implement it ..

+1  A: 

You're running over your input string with strcat. You need to either create a new string for each token, copying the token and "ay", or simply print the token and then "ay". However, if you're using C++ why not use istream iterators and STL algorithms?

On Freund
as of now, i have only read very basic stuff and most of what i know are based on c style pointers and stuff.. so not sure how to use those yet ..
Charles Khunt
+1  A: 

To be honest, I severly doubt the quality of the C++ book, judging from your example. The “basic stuff” in C++ isn't the C pointer style programming. Rather, it's applying high-level library functionality. As “On Freund” pointed out, the C++ standard library provides excellent features to tackle your task. You might want to search for recommendations of better C++ books.

Concerning the problem: your printPigLatin could use the existing function strcpy (or better: strncpy which is safer in regards to buffer overflows). Your manual copy omits the first character from the input because you're using the i + 1st position. You also have a broken loop condition which always tests the same (first) character. Additionally, this should result in an overflow anyway.

Konrad Rudolph
Reason why I thought of using the for loop is because I don't need the first letter of the token and strcpy or strncpy copies them all to the temp array .. im using deitel's c++ how to program 6th ed. I have no c knowledge btw..
Charles Khunt
Okay, Deitel's book *is* extremely bad. In fact, it's on a list of the worst C++ books ever (read the discussion I linked to). Concerning use of `strcpy`: you can tell the function what to copy; in particular, you can tell it to omit the first letter. And by the way, I've got no C knowledge either: it's irrelevant for C++ programming.
Konrad Rudolph
says here that strncpy only copies the first number of characters specified in the argument..
Charles Khunt
Agreed, this is C code compiled in C++, and not particularly elegant.
Matt H
A: 

As the people before me pointed out, there are several other methods of achieving what you want to do.

However, the actual problem with your code seems to be the use of strcat, I see that you changed it a bit in the edit. Here is an explanation of why the initial one did not work char* and size issues

Basically, the pointer does not allocate enough memory to add the "ay" to the string provided. If you create a pointer using the technique shown in the link, it should work fine.

I got your program to work, taking the strcat out and using

cout << word << "ay" << endl

Johannes
A: 

Your loop is infinite because of *word != '\0'.

The word pointer is not changed at any time in the loop.

MickTaiwan
word is the value returned by strtok(). This is a C-String and thus '\0' terminated.
Martin York
word is terminated by \0. Yes.But tell me in this loop where the pointer is incremented ?When *word will be different than \n ?for( int i = 0; *word != '\0'; i++ ) { temp[i] = word[i + 1]; }
MickTaiwan
i tried doing it like this:for( int i = 0; *word != '\0'; word++, i++){ temp[i] = *word;}but it doesn't work.. it prints "gibber" after each of the tokens .. does it have something to do with the size of temp?
Charles Khunt
Your program works with:for( int i = 0; word[i] != '\0'; i++ ) { temp[i] = word[i]; }
MickTaiwan
A: 

This seemed to have worked:

void printPigLatin( char *word )
{
    cout << word + 1 << word[0] << "ay" << endl;
}

Just not sure if it's a good idea to do that.

Charles Khunt
Of course, it is simplier :)cout << word << "ay" << std::endl;
MickTaiwan