C programming The purpose of this assignment is to give you some practice using
ID: 3764569 • Letter: C
Question
C programming
The purpose of this assignment is to give you some practice using characters and Strings. You will create a program, cipher.c, which will encode a message by shifting each letter some number of places. Thus, if the shift is 2, then A becomes C, B becomes D, and so on. Like this:
Surprisingly, you can do this by simply doing arithmetic with characters, but you should reassure C that the result is a character using a cast. If, for example, a letter contains the value 'A', then 'A'+ 2 gives the integer result 67, which you can turn back into a character by saying (char)(letter + 2), giving the value 'C'.
Unfortunately, (char)('Z' + 2) does not give you the letter 'B' (you can see why from the picture above). But if you realize you went past 'Z', you can subtract 26 (so the result is 'Z' + 2 - 26, or 'Z' - 24), and this will give you 'B'.
This also means that if you encode a message with a shift of n, you can decode it with another shift of 26 - n.
Your program should first call a function, processFile(), which reads a message from a file (congress.txt) into a very large character array. The function should convert all of the letters into uppercase characters. You may discard all the punctuation marks, digits, blanks, and anything else from the input sting.
Your program should then accept the amount to shift as an input to a function, cipher(). The input parameter should be 13, although for your testing purposes, you can simplify things by using a shift of 1. Your function should encode each letter by shifting it the correct amount, and put the encoded letters into null-terminated array of characters.
Finally, you should create a function, outputCode(), which outputs the final encoded message in blocks of five letters, ten blocks per line. The last line may be shorter than five blocks, and the last block may be shorter than five letters.
For example, given a shift of 1, the program will turn this:
Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances.
into this:
Points to Remember:
Please do the assignment in C and not C++
Explanation / Answer
If you have any further queries, just get back to me.
#include <stdio.h>
#include <ctype.h>
void processFile(char String[1000])
{
FILE *fp;
char ch;
int i = 0;
fp = fopen("congress.txt", "r");
while(!feof(fp))
{
ch = getc(fp);
if(isalpha(ch))
String[i++] = toupper(ch);
}
String[i] = '';
}
void cipher(char String[1000], int shift)
{
int i = 0;
while(String[i] != '')
{
if(String[i] + shift <= 90)
String[i] = (char)((String[i] + shift));
else
String[i] = String[i] + shift - 26;
i++;
}
}
void outputCode(char String[1000])
{
int i = 0, letters = 0, words = 0;
while(String[i] != '')
{
printf("%c", String[i]);
letters++;
if(letters == 5)
{
printf(" ");
letters = 0;
words++;
}
if(words == 10)
{
printf(" ");
letters = 0;
words = 0;
}
i++;
}
}
int main()
{
char String[1000];
processFile(String);
cipher(String, 1);
outputCode(String);
printf(" ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.