标记化并转换为猪拉丁语

发布于 2024-07-21 06:48:50 字数 1243 浏览 6 评论 0原文

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

书中的练习告诉我将一个句子分成标记,然后将每个标记转换为猪拉丁语,然后显示它们。 .

猪拉丁语在这里基本上是这样的: ball 在 Piglatin 中变成 allboy .. boy 变成 oybay .. 取出第一个字母,放在最后,然后添加“ay” ..

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

#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;
}

我明白标记化部分非常清楚,但我不知道如何做猪拉丁语..我尝试首先简单地将“ay”添加到标记中,看看结果会是什么..不知道为什么程序会进入无限循环并继续显示“ayay”..有什么提示吗?

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

编辑:这就是我“看到”它完成的方式,但不确定如何正确实现它..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

星星的轨迹 2024-07-28 06:48:50

您正在使用 strcat 运行输入字符串。 您需要为每个标记创建一个新字符串,复制标记和“ay”,或者简单地打印标记,然后打印“ay”。 但是,如果您使用 C++,为什么不使用 istream 迭代器和 STL 算法呢?

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?

放血 2024-07-28 06:48:50

说实话,从你的例子来看,我严重怀疑这本C++书的质量。 C++ 中的“基本内容”不是 C 指针风格的编程。 相反,它正在应用高级库功能。 正如“On Freund”指出的那样,C++ 标准库提供了出色的功能来解决您的任务。 您可能想要搜索推荐更好的 C++ 书籍。

关于这个问题:您的 printPigLatin 可以使用现有的函数 strcpy (或更好:strncpy,它在缓冲区溢出方面更安全)。 您的手动副本省略了输入中的第一个字符,因为您使用的是i + 1第一个位置。 您还有一个损坏的循环条件,它总是测试相同的(第一个)字符。 此外,无论如何这都会导致溢出。

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.

离不开的别离 2024-07-28 06:48:50

正如我之前的人指出的那样,还有其他几种方法可以实现您想要做的事情。

然而,您的代码的实际问题似乎是 strcat 的使用,我看到您在编辑中对其进行了一些更改。 以下是为什么第一个不起作用的解释 char* 和大小问题

基本上,指针没有分配足够的内存来将“ay”添加到提供的字符串中。 如果您使用链接中显示的技术创建指针,它应该可以正常工作。

我让你的程序运行起来,取出 strcat 并使用

cout << 词<< “是”<< 结束

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

等风也等你 2024-07-28 06:48:50

由于 *word != '\0',您的循环是无限的。

字指针在循环中任何时候都不会改变。

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

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

幸福不弃 2024-07-28 06:48:50

这似乎有效:

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

只是不确定这样做是否是个好主意。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文