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

Asked

Viewed 96 times

3

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

"Barack Obama" to "OBAMA, Barack".

That is, pro bibliographic format, type when citing authors of books/articles. But I want to know how to convert any name size to this format, can be with 2 names (as in the example above) or with 3, 4, 5... n names, for example:

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

I know how to convert if I know how many names the whole name of the person will have, but I want to know how to do it for any full name size, ie not needing to know how many names the person has in the full name.

Here’s the code I’ve been able to do so far (it works only 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]);
  • A hint: I believe you can use "Strtok" to split the name using empty spaces.

1 answer

2

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.