ramontricolor12

LISTA 08 - Exercício 10

Aug 9th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. /*CABEÇALHO
  2.     Arquivo: LISTA 08 - Exercício 10.c
  3.     Objetivo: Escrever uma função similar a strcmp da biblioteca do C.
  4.     Autor(a): Ramon Borges.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. //PROTOTIPO DE FUNÇÕES
  10. int stringCompara(char *s1, char *s2);
  11.  
  12. int main()
  13. {
  14.     char *s1, *s2;
  15.     int resul;
  16.     s1 = (char*) malloc(50*sizeof(char));
  17.     s2 = (char*) malloc(50*sizeof(char));
  18.  
  19.     printf("Informe a primeira palavra: ");
  20.     gets(s1);
  21.     printf("Informe a segunda palavra: ");
  22.     gets(s2);
  23.     resul = stringCompara(s1, s2);
  24.  
  25.     printf("\nstrcmp (%s , %s) = %d\n", s1, s2, resul);
  26.     if(resul == 1)
  27.         printf("%s e maior que %s\n\n", s1, s2);
  28.     else if(resul == -1)
  29.         printf("%s e maior que %s\n\n", s2, s1);
  30.     else if(!resul)
  31.         printf("%s e igual %s\n\n", s1, s2);
  32.     free(s1);
  33.     free(s2);
  34.     return 0;
  35. }
  36.  
  37. int stringCompara(char *s1, char *s2)
  38. {
  39.     do
  40.     {
  41.         if(*s1 > *s2)
  42.             return 1;
  43.         else if(*s1 < *s2)
  44.             return -1;
  45.         s1++;
  46.         s2++;
  47.     }while(*(s1-1) != '\0' && *(s2-1) != '\0');
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment