I need help with my programming assignment, I have everything done except for one part! Here is the assignment:
Problem 1: String Operations and Data Security: To improve data security in the transmission of data and information, dynamic character coding is being practiced. The modification of the original characters can be using the first 8 prime members [1, 2, 3, 5, 7, 11, 13, 17]: First character enhanced by 1; second character by 2, third by 3, .. 8th character by 17. Next 8 characters use the prime numbers in the reverse order 17..1, and decrease the values. Use a total message of at least 64 characters [in quantities of 8 characters] and repeat the process of modifying 1-17 for the first 8; modifying by 17 -1 for next 8, and so on. Make your own message. After the message is coded, decoding should also be done, to restore the original message. You may want to change the lower case and upper case transitions as well.
Example: Original Message A 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 ……
I'm having trouble printing out the prime numbers without messing up the code afterwards such as the encoded and decoded ASCII codes and the encoded and decoded codes. This is my code so far:
If you could help me somehow that would be great! I would really appreciate it, thank you.
size_t x;
int i, c;
i = 1;
c = 0;
char text[65];
int s[8] = {1, 2, 3, 5, 7, 11, 13, 17};
printf("Enter a line of text: "); // prompts user to enter text
fgets(text, 65, stdin); // reads input from user
printf("\nOriginal Message: ");
for(x = 0; x < strlen(text); ++x) // for loop to print original message
{
printf("%c", text[x]);
} // end for
// ASCII Code
printf("\nASCII Code: ");
for(x = 0; x < strlen(text) - 1; ++x)
{
printf("%d ", text[x]);
}
// prime numbers
// my issue is 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 for
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;
i = 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 for
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 are represented by numbers. For example, for ASCII,
z
is represented by the number 122. If you add something to that value then you get a new value (e.g.122 + 17 = 139
). That new value may not be valid ASCII (e.g. 128 or larger), and might be a control code (e.g. 127 is the "delete" control character).Because the values may no longer represent valid characters, you can't print them as characters. You need to print them as values (e.g.
for(x = 0; text[x] != 0; x++) { printf("%d ", text[x]); }
).Worse, for "signed char", the addition can cause overflow, which is undefined behaviour in C. You'd need to use a different data type (e.g.
unsigned char
oruint8_t
) to ensure the code has defined/expected behaviour.Also note that your long chain of "if .. else if ... else if" statements should be a "switch()" with cases; more like this:
However; by using
int s[16] = {1, 2, 3, 5, 7, 11, 13, 17, 17, 13, 11, 7, 5, 3, 2, 1};
you'd be able replace many lines of similar code with something like this: