繁体   English   中英

C++小猪拉丁语翻译器

[英]C++ pig latin translator

我正在用 C++ 编写一个程序,将一个句子翻译成猪拉丁语,只需取单词的第一个字母并将其放在最后,然后添加“ay”,例如“hello world”变成“ellohay orldway”,但它不起作用,我尝试了几种不同的方法都没有奏效,此时我即将放弃,请有人帮助我正确地做到这一点。

#include "stdafx.h"

#include <iostream>
#include <string>
using namespace std;

void seperator(char []);

int main()
{
    string input;
    const int SIZE = 40;
    char sent[SIZE];

    cout << " enter sentence " << endl;
    cin >> input;
    cin.getline(sent, SIZE);
    seperator(sent);
    cout << input << endl;


    system ("pause");
    return 0;
}

void seperator(char input[])
{
    int count = 0;
    char x;
    while (input[count] != '\0')
        count++;
    if (isalpha(input[count]))
        x =input[count];
    if (input[count] == ' ')
        input[count] = (x + ' ');
    if (input[count] == ' ')
        input[count] = 'ay';


}

您的cin >> input; 只是让字符串直到第一个空格,所以helloworld之间的空间。

这就是为什么它似乎要删除除第一个单词之外的所有内容,您应该使用std::getline (std::cin,input);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM