How to convert a normal whole name to its bibliographic format in c?

Question:

I want to know how to convert, for example, the following string:

"Barack Obama" for "OBAMA, Barack".

In other words, for bibliographic format, like when they mention authors of books/articles. But I want to know how to convert any name length to this format, it can be with 2 names (like in the example above) or with 3, 4, 5… n names, for example:

Lionel Andrés Blablabla Soccer Player Messi This name would look like this: MESSI, Lionel Andrés Blablabla Soccer Player.

I know how to convert if I know how many names the person's full name will have, but I want to know how to do this for any full name length, ie not needing to know how many names the person has in the full name.

Here's the code I've managed to do so far (it only works for whole names that have 6 names in total):

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]);

Answer:

char* nome="Leonel da Silva Messi";

char* aux=strdup(nome);                // aux = cópia do nome
char* ue=strrchr (aux,' ');            // apontador para o ultimo espaço
*ue='\0';                              // aux = "Leonel da Silva\0Messi"
printf("%s, %s\n", ue+1, aux);         // ... ou strupr(ue+1) se estveres em MS
Scroll to Top