简体   繁体   English

如何在C中将全名转换为书目格式名称?

[英]How to convert a full name to a bibliographic format name in C?

I want to know how to convert, for example, this string: 我想知道如何转换,例如,这个字符串:

"Barack Obama" to "OBAMA, Barack". “巴拉克奥巴马”改为“奥巴马,巴拉克”。

Ie, to bibliographic format, like when someone writes a book/article. 即,书目格式,就像有人写书/文章一样。 But I want to know how to convert any kind of name in this format, it can be 2 names (as the case before) or 3, 4, 5... n names, for example: 但我想知道如何转换这种格式的任何类型的名称,它可以是2个名称(如前所述)或3,4,5 ... n名称,例如:

Lionel Andrés Blablabla Soccer Player Messi LionelAndrésBlablabla足球运动员梅西

This name would be: MESSI, Lionel Andrés Blablabla Soccer Player. 这个名字将是:MESSI,LionelAndrésBlablabla足球运动员。

I know how to do it if I know how much names the full name will have, but I want to know how I can do it for a full name that I don't know how many names it will have. 如果我知道全名会有多少名字,我知道怎么做,但是我想知道我怎么能用全名来做,我不知道它会有多少名字。

Here is what I've got so far (working only for 6 names in the example below): 这是我到目前为止所做的事情(仅在下面的例子中为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]);

If the format is relatively simple (you seem to indicate so with your comment "any kind of name in this format"), you can use the following approach: 如果格式相对简单(您似乎用注释“ 以此格式显示任何类型的名称”),则可以使用以下方法:

  1. Use strrchr() to locate the last space in the string. 使用strrchr()来定位字符串中的最后一个空格。 Obviously, characters beyond that point constitute the surname. 显然,超过这一点的人物构成了姓氏。
  2. Therefore characters before that point constitute all names that are not part of the surname. 因此, 该点之前的字符构成所有属于姓氏的名称。
  3. So you simply print out the last name, a comma and space, then the earlier part of the string. 所以你只需打印出姓氏,逗号和空格,然后打印字符串的前一部分。

Now, granted, there are all sorts of edge cases that you may have to handle if your input strings aren't so nicely formatted, things like trailing spaces, surnames that consist of more than one word, and so on. 现在,如果您的输入字符串格式不是很好,可能需要处理各种边缘情况,例如尾随空格,包含多个单词的姓氏等等。


In terms of actual code, the following program would be a good starting point: 就实际代码而言,以下程序将是一个很好的起点:

#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;
}

A sample run of this is shown below: 此示例运行如下所示:

Enter name> bob
        --> bob

Enter name> allan
        --> allan

Enter name> Barack Obama
        --> OBAMA, Barack

Enter name> Lionel Andres Blablabla Soccer Player Messi
        --> MESSI, Lionel Andres Blablabla Soccer Player

Enter name> <CTRL-D pressed>
*** End of file, stopping.

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

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