IvoSilva

[PROG1] Ficha 10 | Exercício 3.3

Jan 9th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LIMIT 100
  4. char converte(char c , int shift)
  5. {
  6.     unsigned char d = c ;
  7.     d = d + shift ;
  8.     if (d > 'z') d = ('a' - 1) + (d - 'z') ;
  9.     else if (d < 'a') d = ('z' + 1) - ('a' - d) ;
  10.     return (d) ;
  11. }
  12. void desloca(char texto[] , char cifra[] , int shift)
  13. {
  14.     int i ;
  15.     for (i = 0 ; i < strlen (texto) - 1 ; i++)
  16.     {
  17.         if (isalpha(texto[i]))cifra[i] = converte(texto[i] , shift) ;
  18.         else cifra[i] = texto[i] ;
  19.     }
  20.     cifra[strlen (texto) - 1] = '\0' ;
  21. }
  22. void main()
  23. {
  24.     char txt[LIMIT] , cifra[LIMIT] ;
  25.     int i ;
  26.     printf ("Cifra? ") ;
  27.     fgets (txt , LIMIT , stdin) ;
  28.     for (i = 1 ; i < 26 ; i++)
  29.     {
  30.         desloca (txt , cifra , i) ;
  31.         printf ("com shift + %d : %s\n" , i , cifra) ;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment