Advertisement
hmcristovao

lista05_exerc22

Oct 3rd, 2012
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_NOMES 500
  4. #define MAX_CAR 51
  5. int main() {
  6.    char  nomes[MAX_NOMES][MAX_CAR];
  7.  
  8.    int i=0;
  9.    printf("Digite os nomes (fim para terminar)\n");
  10.    do {
  11.       gets(nomes[i]);
  12.       i++;
  13.    }while(strcmp(nomes[i-1],"fim")!=0 && i<MAX_NOMES);
  14.    i--;  //  ajusta para a qtde efetiva de nomes cadastrados
  15.    printf("\nRelacao de nomes digitados:\n");
  16.    int n;
  17.    for(n=0; n<i; n++)
  18.       printf("%s \n",nomes[n]);
  19.  
  20.    printf("\nRelacao de nomes em ordem alfabetica:\n");
  21.    char aux[MAX_CAR];
  22.    int troca = 1; // flag para indicar se houve troca
  23.    while(troca) {
  24.       troca = 0;
  25.       for(n=0; n<i-1; n++) {
  26.           if(strcmp(nomes[n],nomes[n+1]) > 0) {
  27.              strcpy(aux,nomes[n]);        // não aceita: aux = nomes[n];
  28.              strcpy(nomes[n],nomes[n+1]); // não aceita: nomes[n] = nomes[n+1];
  29.              strcpy(nomes[n+1],aux);      // não aceita: nomes[n+1] = aux;
  30.              troca = 1;
  31.           }
  32.       }
  33.    }
  34.    for(n=0; n<i; n++)
  35.       printf("%s \n",nomes[n]);
  36.  
  37.    return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement