Arrays in C and Prime Numbers -


i need programming assignment, have done except 1 part! here assignment:

problem 1: string operations , data security: improve data security in transmission of data , information, dynamic character coding being practiced. modification of original characters can using first 8 prime members [1, 2, 3, 5, 7, 11, 13, 17]: first character enhanced 1; second character 2, third 3, .. 8th character 17. next 8 characters use prime numbers in reverse order 17..1, , decrease values. use total message of @ least 64 characters [in quantities of 8 characters] , repeat process of modifying 1-17 first 8; modifying 17 -1 next 8, , on. make own message. after message coded, decoding should done, restore original message. may want change lower case , upper case transitions well.

   example:  original message               b     c    d. …..                      normal ascii      65    66    67   68   ….                      prime numbers      1     2     3    5   ….                                enhanced ascii    66    68    70   73 ….                      coded message      b     d     f      …… 


i'm having trouble printing out prime numbers without messing code afterwards such encoded , decoded ascii codes , encoded , decoded codes. code far: if me somehow great! appreciate it, thank you.

size_t x; int i, c;  = 1; c = 0;  char text[65]; int s[8] = {1, 2, 3, 5, 7, 11, 13, 17};  printf("enter line of text: "); // prompts user enter text fgets(text, 65, stdin); // reads input user printf("\noriginal message: "); for(x = 0; x < strlen(text); ++x) // loop print original message {     printf("%c", text[x]); } // end  // ascii code printf("\nascii code: "); for(x = 0; x < strlen(text) - 1; ++x) {     printf("%d ", text[x]); }  // prime numbers // issue here printf("\n\nprime numbers: "); for(x = 0; x <= 8 ; ++x) {     if(c == 0)     {         if(i == 1)         {             printf("1 ");             ++i;         }         else if(i == 2)         {             printf("2 ");             ++i;         }         else if(i == 3)         {             printf("3 ");             ++i;         }         else if(i == 4)         {             printf("5 ");             ++i;         }         else if(i == 5)         {             printf("7 ");             ++i;         }         else if(i == 6)         {             printf("11 ");             ++i;         }         else if(i == 7)         {             printf("13 ");             ++i;         }         else if(i == 8)         {             printf("17 ");             ++c;             ++x;         }     }     if(c == 1)     {         if(i == 8)         {             printf("17 ");             --i;         }         else if(i == 7)         {             printf("13 ");             --i;         }         else if(i == 6)         {             printf("11 ");             --i;         }         else if(i == 5)         {             printf("7 ");             --i;         }         else if(i == 4)         {             printf("5 ");             --i;         }         else if(i == 3)         {             printf("3 ");             --i;         }         else if(i == 2)         {             printf("2 ");             --i;         }         else if(i == 1)         {             printf("1 ");             --c;         }     } // end outer if } // issue ends here   for(x = 0; x < strlen(text) - 1; ++x) {     if(c == 0) // outer if statement increasing     {         if(i == 1) // inner if statement         {             text[x] = text[x] + 1;             ++i;         }         else if (i == 2)         {             text[x] = text[x] + 2;             ++i;         }         else if (i == 3)         {             text[x] = text[x] + 3;             ++i;         }         else if(i == 4)         {             text[x] = text[x] + 5;             ++i;         }         else if(i == 5)         {             text[x] = text[x] + 7;             ++i;         }         else if(i == 6)         {             text[x] = text[x] + 11;             ++i;         }         else if(i == 7)         {             text[x] = text[x] + 13;             ++i;         }         else if(i == 8)         {             text[x] = text[x] + 17;             ++c;             ++x;         } // end if statement     } // end outer if statement     if(c == 1) // outer if statement decreasing     {         if(i == 8)         {             text[x] = text[x] + 17;             --i;         }         else if(i == 7)         {             text[x] = text[x] + 13;             --i;         }         else if(i == 6)         {             text[x] = text[x] + 11;             --i;         }         else if(i == 5)         {             text[x] = text[x] + 7;             --i;         }         else if(i == 4)         {             text[x] = text[x] + 5;             --i;         }         else if(i == 3)         {             text[x] = text[x] + 3;             --i;         }         else if(i == 2)         {             text[x] = text[x] + 2;             --i;         }         else if(i == 1)         {             text[x] = text[x] + 1;             --c;         }     } // end outer if } // end printf("\n\nencrypted message: "); for(x = 0; x <= strlen(text) - 1; ++x) {    printf("%c", text[x]); }  printf("\nencrypted ascii: "); for(x = 0; x < strlen(text) - 1; x++) {     printf("%d ", text[x]); }  c = 0; = 1;  for(x = 0; x < strlen(text) - 1; ++x) {     if(c == 0)     {         if(i == 1)         {             text[x] = text[x] - 1;             ++i;         }         else if(i == 2)         {             text[x] = text[x] - 2;             ++i;         }         else if(i == 3)         {             text[x] = text[x] - 3;             ++i;         }         else if(i == 4)         {             text[x] = text[x] - 5;             ++i;         }         else if(i == 5)         {             text[x] = text[x] - 7;             ++i;         }         else if(i == 6)         {             text[x] = text[x] - 11;             ++i;         }         else if(i == 7)         {             text[x] = text[x] - 13;             ++i;         }         else if(i == 8)         {             text[x] = text[x] - 17;             ++c;             ++x;         } // end if statement     } // end outer if statement     if(c == 1)     {         if(i == 8)         {             text[x] = text[x] - 17;             --i;         }         else if(i == 7)         {             text[x] = text[x] - 13;             --i;         }         else if(i == 6)         {             text[x] = text[x] - 11;             --i;         }         else if(i == 5)         {             text[x] = text[x] - 7;             --i;         }         else if(i == 4)         {             text[x] = text[x] - 5;             --i;         }         else if(i == 3)         {             text[x] = text[x] - 3;             --i;         }         else if(i == 2)         {             text[x] = text[x] - 2;             --i;         }         else if(i == 1)         {             text[x] = text[x] - 1;             --c;         } // end inner if statements     } // end outer if statements } // end  printf("\n\ndecrypted message: "); for(x = 0; x < strlen(text); ++x) {     printf("%c", text[x]); }  printf("\ndecrypted ascii: "); for(x = 0; x < strlen(text) - 1; ++x) {     printf("%d ", text[x]); } 

puts(" "); } // end main

characters represented numbers. example, ascii, z represented number 122. if add value new value (e.g. 122 + 17 = 139). new value may not valid ascii (e.g. 128 or larger), , might control code (e.g. 127 "delete" control character).

because values may no longer represent valid characters, can't print them characters. need print them values (e.g. for(x = 0; text[x] != 0; x++) { printf("%d ", text[x]); }).

worse, "signed char", addition can cause overflow, undefined behaviour in c. you'd need use different data type (e.g. unsigned char or uint8_t) ensure code has defined/expected behaviour.

also note long chain of "if .. else if ... else if" statements should "switch()" cases; more this:

    switch(i) {         case 1:             text[x] = text[x] + 1;             ++i;             break;         case 2:             text[x] = text[x] + 2;             ++i;             break;         ...     } 

however; using int s[16] = {1, 2, 3, 5, 7, 11, 13, 17, 17, 13, 11, 7, 5, 3, 2, 1}; you'd able replace many lines of similar code this:

    for(x = 0; text[x] != 0; x++) {         text[x] += s[x % 16]);     } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -