IvoSilva

[PROG1] Ficha 10 | Exercício 3.2

Jan 9th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 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.     c += shift ;
  7.     if (c > 'z') c = ('a' - 1) + (c - 'z') ;
  8.     else if (c < 'a') c = ('z' + 1) - ('a' - c) ;
  9.     return (c) ;
  10. }
  11. void desloca(char texto[] , char cifra[] , int shift)
  12. {
  13.     int i ;
  14.     for (i = 0 ; i < strlen (texto) ; i++)
  15.     {
  16.         if (isalpha(texto[i])) cifra[i] = converte(texto[i] , shift) ;
  17.         else cifra[i] = texto[i] ;
  18.     }
  19.     cifra[strlen (texto)] = '\0' ;
  20. }
  21. void main()
  22. {
  23.     char txt[LIMIT] , cifra[LIMIT] ;
  24.     int shift ;
  25.     printf ("Texto? ") ;
  26.     fgets (txt , LIMIT , stdin) ;
  27.     printf ("Shift? ") ;
  28.     scanf ("\n%d" , &shift) ;
  29.     desloca (txt , cifra , shift) ;
  30.     printf ("Cifra: %s\n" , cifra) ;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment