-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextraAssignment.c
More file actions
159 lines (124 loc) · 3.56 KB
/
extraAssignment.c
File metadata and controls
159 lines (124 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Author: Dinel Anthony
* Student Number: 20189775
* Email: dinela@student.ubc.ca
* Date: June 2, 2021
* Purpose: To encode and decode Whatsapp messages. I made lab 4 a bit more complex because it seemed
* like a really fun assignment. I added extra features, like a loop for more conversions, and the ability
* to enter your own message to decode, or encode.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//Set the maximum amount of characters or numbers to decode/encode
#define MAX_SIZE 100
//Function prototypes
void decode(int encoded[], char decoded[]);
void encode(char decoded[], int encoded[]);
int main(void)
{
int input[MAX_SIZE];
int output[MAX_SIZE];
int choice;
int repeat = 1;
while (repeat == 1)
{
//Prompts user to choose whether they would like to encode or decode a message
printf("Please choose an option below:\n");
printf("Option 1: Decode Set of Numbers\n");
printf("Option 2: Encode Message\n");
printf("Choice: ");
scanf("%d", &choice);
//Calls the decode function
if (choice == 1)
{
decode(input, output);
}
//Calls the encode function
else if (choice == 2)
{
encode(input, output);
}
//Loop for an invalid input
else
{
printf("ERROR: Invalid input\n");
}
//Prompts the use with the option to compute another encryption/decryption
printf("Would you like to try another encryption/decryption?\n");
printf("Enter 1 for YES and 0 for NO: ");
scanf("%d", &repeat);
//Repeats the program if the user chooses yes, and ends if the user chooses no
if (repeat == 1)
{
continue;
}
else
{
printf("Understandable, have a nice day.\n");
return 0;
}
}
return 0;
}
//Function to decode numbers into ASCII text
void decode(int encoded[], char decoded[])
{
int counter;
//Prompts user to input the decoded message
printf("Please enter the numbers to decode. \n");
printf("Remember that entering 0 will mark the end of the message: \n");
for (counter = 0; counter < MAX_SIZE; counter++)
{
//Checks for user input and stores it as a character
scanf("%d", &encoded[counter]);
decoded[counter] = encoded[counter];
//Ends the loop as soon as the message is over (entering 0)
if (decoded[counter] == 0)
{
counter = MAX_SIZE;
}
}
printf("This translates to:\n");
//Prints each character of the newly decoded message
for (counter = 0; counter < MAX_SIZE; counter++)
{
//Ends loop as soon as the message is over (entering 0)
if (decoded[counter] == 0)
{
counter = MAX_SIZE;
}
printf("%c", decoded[counter]^42);
}
printf("\n\n");
}
//Function to encode ASCII text to numbers
void encode(char decoded[], int encoded[])
{
int counter;
//Prompts user to type the message to be encoded
printf("Please enter the message to encode. \n");
printf("Remember to type 0 to end the message:\n");
for (counter = 0;counter < MAX_SIZE;counter++)
{
//Checks for user input and stores each value as a number
scanf("%c", &decoded[counter]);
encoded[counter] = decoded[counter];
//Ends loop early if the ASCII text "0" is entered
if (encoded[counter] == 48)
{
counter = MAX_SIZE;
}
}
printf("This translates to:\n");
//Prints each number of the new encoded message
for (counter = 0;counter < MAX_SIZE;counter++)
{
printf("%d ", encoded[counter]^5);
//Ends the loop when the ASCII text "0" is detected
if (encoded[counter] == 48)
{
counter = MAX_SIZE;
}
}
printf("\n\n");
}