C++ 转换成拉丁语
需要一些将字符串转换为拉丁语的帮助。编写一个程序,输入两个字符串变量,第一个和最后一个,用户应输入每个字符串变量及其名称。首先,将两个字符串都转换为大写。然后,您的程序应该创建一个新字符串,其中包含拉丁语的全名。 这是我的主要职责:C++ 转换成拉丁语,c++,cstring,C++,Cstring,需要一些将字符串转换为拉丁语的帮助。编写一个程序,输入两个字符串变量,第一个和最后一个,用户应输入每个字符串变量及其名称。首先,将两个字符串都转换为大写。然后,您的程序应该创建一个新字符串,其中包含拉丁语的全名。 这是我的主要职责: char* first = new char[MAX]; char* last = new char[MAX]; char* full = new char[MAX*2]; cout << "Enter first name: "; cin.getli
char* first = new char[MAX];
char* last = new char[MAX];
char* full = new char[MAX*2];
cout << "Enter first name: ";
cin.getline(first, MAX, '\n');
cout << "Enter last name: ";
cin.getline(last, MAX, '\n');
for(int i = 0; first[i]; i++)
{
first[i] = toupper(first[i]);
}
for(int i = 0; last[i]; i++)
{
last[i] = toupper(last[i]);
}
transformPigLatin(first);
transformPigLatin(last);
int offset = 0;
int i;
for (i=0; i<MAX && first[i]!=0; i++)
full[offset++]=first[i];
full[offset++]=' ';
for (i=0; i<MAX && last[i]!=0; i++)
full[offset++]=last[i];
transformPigLatin(full);
cout << full << endl;
char*first=新字符[MAX];
char*last=新字符[MAX];
char*full=新字符[MAX*2];
CUT< P>你在C++中编码,所以你应该使用C++字符串而不是C字符串。使用C++字符串的情况要简单得多。该类有许多用于操作字符串内容的方法,包括和
试试这个:
#include <string>
#include <algorithm>
std::string first;
std::string last;
std::string full;
std::cout << "Enter first name: ";
std::getline(std::cin, first);
std::cout << "Enter last name: ";
std::getline(std::cin, last);
std::transform(first.begin(), first.end(), first.begin(), ::toupper);
std::transform(last.begin(), last.end(), last.begin(), ::toupper);
transformPigLatin(first);
transformPigLatin(last);
full = first + " " + last;
std::cout << full << std::endl;
void拉丁语(char*word)
{
int长度=strlen(字);
如果(长度==0)返回;
char ch=字[0];
如果((ch<'A')|(ch>'Z'))返回;
如果(strhr(“AEIOU”,ch)!=NULL)
{
如果((长度+3)>=MAX)返回;
strcat(单词“方式”);
}
其他的
{
如果((长度+2)>=MAX)返回;
如果(长度>1)
{
对于(int i=1;i
我喜欢你解决问题的方式,谢谢。问题是我必须只使用C字符串..这就是为什么我感到困惑的原因@USER 360652:如果你的课堂正在教他们,他们正在教C++,就像20年前一样。世界上其他地方都在进步。除了使用<代码> CIN < /C>和 CUT,其余代码根本不使用C++,而是使用C。我在我的答案中添加了一个C示例。
#include <string>
#include <algorithm>
std::string first;
std::string last;
std::string full;
std::cout << "Enter first name: ";
std::getline(std::cin, first);
std::cout << "Enter last name: ";
std::getline(std::cin, last);
std::transform(first.begin(), first.end(), first.begin(), ::toupper);
std::transform(last.begin(), last.end(), last.begin(), ::toupper);
transformPigLatin(first);
transformPigLatin(last);
full = first + " " + last;
std::cout << full << std::endl;
void transformPigLatin(std::string &word)
{
if (word.empty()) return;
char ch = word[0];
if ((ch < 'A') || (ch > 'Z')) return;
if (std::string("AEIOU").find(ch) != std::string::npos)
{
word += "WAY";
}
else
{
if (word.length() > 1)
{
word.erase(0, 1);
word.append(&ch, 1);
}
word += "AY";
}
}
int getLine(char *prompt, char *word, int maxlen)
{
if (prompt != NULL)
{
printf("%s", prompt);
fflush(stdout);
}
if (fgets(word, maxlen, stdin) == NULL)
{
*word = '\0';
return 0;
}
int length = strlen(word);
if (word[length-1] == '\n')
{
word[length-1] = '\0';
--length;
}
else
{
int ch;
while (((ch = getchar()) != '\n') && (ch != EOF));
}
return length;
}
char* first = new char[MAX];
char* last = new char[MAX];
char* full = new char[(MAX*2)+1];
getLine("Enter first name: ", first, MAX);
getLine("Enter last name: ", last, MAX);
for(int i = 0; first[i]; i++)
{
first[i] = toupper(first[i]);
}
for(int i = 0; last[i]; i++)
{
last[i] = toupper(last[i]);
}
transformPigLatin(first);
transformPigLatin(last);
strcpy(full, first);
strcat(full, " ");
strcat(full, last);
printf("%s\n", full);
void transformPigLatin(char* word)
{
int length = strlen(word);
if (length == 0) return;
char ch = word[0];
if ((ch < 'A') || (ch > 'Z')) return;
if (strchr("AEIOU", ch) != NULL)
{
if ((length+3) >= MAX) return;
strcat(word, "WAY");
}
else
{
if ((length+2) >= MAX) return;
if (length > 1)
{
for (int i = 1; i < length; ++i)
word[i-1] = word[i];
word[length-1] = ch;
}
strcat(word, "AY");
}
}