标记化并转换为猪拉丁语
这看起来像是家庭作业,但请放心,这不是家庭作业。 只是我们在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在使用 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?
说实话,从你的例子来看,我严重怀疑这本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 functionstrcpy
(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 thei + 1
st position. You also have a broken loop condition which always tests the same (first) character. Additionally, this should result in an overflow anyway.正如我之前的人指出的那样,还有其他几种方法可以实现您想要做的事情。
然而,您的代码的实际问题似乎是 strcat 的使用,我看到您在编辑中对其进行了一些更改。 以下是为什么第一个不起作用的解释 char* 和大小问题
基本上,指针没有分配足够的内存来将“ay”添加到提供的字符串中。 如果您使用链接中显示的技术创建指针,它应该可以正常工作。
我让你的程序运行起来,取出 strcat 并使用
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
由于 *word != '\0',您的循环是无限的。
字指针在循环中任何时候都不会改变。
Your loop is infinite because of *word != '\0'.
The word pointer is not changed at any time in the loop.
这似乎有效:
只是不确定这样做是否是个好主意。
This seemed to have worked:
Just not sure if it's a good idea to do that.