标记化和转换为猪拉丁 [英] tokenizing and converting to pig latin

查看:190
本文介绍了标记化和转换为猪拉丁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来像家庭作业的东西,但请放心,它不是家庭作业。只是在我们在我们的c ++课程中使用的书中的一个练习,我正在尝试阅读指针。



书中的练习告诉我分裂一个句子然后将它们转换成猪拉丁然后显示它们。



猪拉丁这里基本上是这样的:球变成allboy在piglatin ..男孩变成oybay。 。



到目前为止,这是我有的:

  #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句[500];
char * token;

cout<< 输入字符串to tokenize和转换:;
cin.getline(sentence,500);

token = strtok(sentence,);

cout<< \\\
Pig 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<<温度< endl;
}



我理解标记化部分很清楚,但我不知道如何做我试图通过简单地添加ay到令牌开始,看看什么结果将是..不知道为什么程序进入无限循环,并继续显示ayay..任何提示? / p>

编辑:现在这个工作正常,但不知道如何在添加ay之前添加令牌的第一个字母



EDIT:这是我如何看到它,但不知道如何正确实现它..

解决方案

老实说,我从你的例子判断,我怀疑C ++书的质量。 C ++中的基本东西不是C指针样式编程。相反,它正在应用高级库功能。如On Freund指出,C ++标准库提供了优秀的功能来处理你的任务。您可能希望搜索更好的C ++图书的建议



关于这个问题:你的 printPigLatin 可以使用现有的函数 strcpy code> strncpy ,这在缓冲区溢出方面更安全)。由于您使用 i + 1 st位置,您的手动复制会省略输入中的第一个字符。你也有一个破坏的循环条件,它总是测试相同的(第一个)字符。此外,这应该会导致溢出。


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 ..

解决方案

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.

这篇关于标记化和转换为猪拉丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆