I'm doing a program to alphabetize and show the last name of the alphabetical order, with the end of the EOF file, only it only works if I do not put names without space, if I put a name with space it simply to.
Example input:
Wow, how beautiful the sky is.
The sun
Output
The sun
My code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct p
{
char nome[100];
};
int ordena(const void *a, const void *b)
{
int r = strcasecmp((*(struct p*)a).nome, (*(struct p *)b).nome);
if (r == 0)
{
return 0;
}
else if(r < 0)
{
return -1;
}
else
{
return 1;
}
}
int main(int argc, char** argv)
{
char teste[1000];
int i = 0;
struct p nomes[2000];
while(scanf("%[^\n]", teste) != EOF)
{
strcpy(nomes[i].nome, teste);
i++;
}
qsort(nomes, i, sizeof(struct p), ordena);
int soma = i - 1;
printf("%s\n", nomes[soma].nome);
return 0;
}