我想知道如何转换,例如,这个字符串:
“巴拉克奥巴马”改为“奥巴马,巴拉克”。即书目格式,就像有人写书/文章一样。但我想知道如何转换这种格式的任何类型的名称,它可以是2个名称(如前所述)或3,4,5 ... n个名称,例如:
LionelAndrésBlablabla足球运动员梅西
这个名字将是:MESSI,LionelAndrésBlablabla足球运动员。
如果我知道全名会有多少名字,我知道怎么做,但我想知道我怎么能用全名来做,我不知道它会有多少名字。
这是我到目前为止所做的事情(仅在下面的例子中为6个名字工作):
char nome[30][100];
int i, j;
for(i = 0; i < 6; i++)
scanf("%s", nome[i]);
for(j = 5; j > 4; j--)
printf("%s,", strupr(nome[j]));
for(i = 0; i <= 4; i++)
printf("%s ", nome[i]);
答案 0 :(得分:0)
如果格式相对简单(您似乎用您的评论和#34表示; 此格式中的任何类型的名称&#34;),您可以使用以下方法:
.parent {
width : 35%;
height : 200px;
background-color : red;
display : inline-block;
margin : 30px auto
}
.parent div {
display : inline-block;
width:30%;
}
找到字符串中的最后一个空格。显然,超出这一点的人物构成了姓氏。现在,如果你的输入字符串没有很好地格式化,比如尾随空格,包含多个单词的姓氏等等,你可能需要处理各种边缘情况。
就实际代码而言,以下程序将是一个很好的起点:
strrchr()
此示例运行如下所示:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
static char buffer[1000];
while (1) {
// Get line from user.
printf ("\nEnter name> ");
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
printf("\n*** End of file, stopping.\n");
return 0;
}
// Remove any trailing newline.
if ((*buffer != '\0') && (buffer[strlen(buffer) - 1] == '\n'))
buffer[strlen(buffer) - 1] = '\0';
// Find last space. If none, output as is.
char *lastSpace = strrchr(buffer, ' ');
if (lastSpace == NULL) {
printf (" --> %s\n", buffer);
continue;
}
// Otherwise separate, capitalise surname and output all.
*lastSpace++ = '\0';
for (char *surchar = lastSpace; *surchar != '\0'; surchar++)
*surchar = toupper(*surchar);
printf (" --> %s, %s\n", lastSpace, buffer);
}
return 0;
}