我需要输入3用逗号将单个字符串分解为多个字符串C++?
全称1分离全名:约翰史密斯,弗林
全名2:沃尔特·肯尼迪,罗伯特
全名3:萨姆,低音,克林顿
然后输出像这样
名1:约翰
名2:沃尔特
名3:山姆
中间名1:史密斯
中间名2:肯尼迪
中间名3:低音
姓1:弗林
姓2:罗伯茨
姓3:克林顿
我如何做这些? 到目前为止,这是我的代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main() {
char first[3][100];
char middle[3][100];
char last[3][100];
char full[3][100];
int i;
cout << "Enter 3 Full Names <first, middle and last names separated by comma>:" << endl;
for (i=0; i<3; i++) {
cout << "Full Name " << i+1 << ":" ;
gets (full[i]);
}
cout << "The first names are: " << endl;
for (i=0; i<3; i++) {
strcpy (first[i], full[i]);
if (strcmp (first[i], ", ")) {
cout << "First Name "<< i+1 << ":" ;
strcpy (first[i], full[i]);
cout << (first[i]);
cout << endl;
}
}
cout << "The middle names are: " << endl;
for (i=0; i<3; i++) {
cout << "Middle Name "<< i+1 << ":" ;
cout << (middle[i]);
cout << endl;
}
cout << "The last names are: " << endl;
for (i=0; i<3; i++) {
cout << "Last Name "<< i+1 << ":" ;
cout << (last[i]);
cout << endl;
}
system("pause");
return 0;
}
哪里标记[作业]? – p4553d
添加了作业标签。 – Tom
我们不允许使用矢量 刚的iostream,为c_string和cstdio! 有人可以张贴一个字符串,然后将它分成3个多串的例子吗? 我只是学习C++所以请简化它 谢谢。 – 2011-02-23 14:27:08