【发布时间】:2011-02-23 08:04:40
【问题描述】:
我需要输入3个用逗号分隔的全名
全名 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;
}
【问题讨论】:
-
标签[作业]在哪里?
-
我们不允许使用vector 只是iostream、cstring和cstdio!有人可以发布一个单个字符串的示例,然后将其分成 3 个多个字符串吗?我只是学习 C++,所以请简化一下谢谢。