fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. void caesar( const char *src, char *dst ) {
  6. int i;
  7. for( i = 0; src[i] != '\0'; ++i ) {
  8. if( src[i] >= 'a' && src[i] <= 'z' ) {
  9. dst[i] = 'a' + ( src[i]-'a' + i + 1 ) % 26;
  10. }
  11. else if( src[i] >= 'A' && src[i] <= 'Z' ) {
  12. dst[i] = 'A' + ( src[i]-'A' + i + 1 ) % 26;
  13. }
  14. else {
  15. fprintf( stderr, "err\n" );
  16. }
  17. }
  18. dst[i]='\0';
  19. }
  20.  
  21. void raseac( const char *src, char *dst ) {
  22. int i;
  23. int l = strlen(src);
  24. char *work = malloc( l );
  25.  
  26. dst[l] = '\0';
  27. while(1) {
  28. for( i = 0; i < l; ++i ) {
  29. int c = rand( ) % 52;
  30. dst[i] = ( c < 26 )? ('a'+c): ('A'+c-26 );
  31. }
  32. caesar( dst, work );
  33. if( strcmp( work, src ) == 0 ) {
  34. free(work);
  35. return;
  36. }
  37. }
  38. }
  39.  
  40. int main(){
  41. char src[] = "text";
  42. char encripted[sizeof(src)];
  43. char decripted[sizeof(src)];
  44.  
  45. caesar( src, encripted );
  46. raseac( encripted, decripted );
  47.  
  48. printf( "src=%s, encripted=%s, decripted=%s\n", src, encripted, decripted );
  49. };
Runtime error #stdin #stdout 0.1s 1852KB
stdin
Standard input is empty
stdout
src=text, encripted=ugax, decripted=text