Created
November 6, 2019 01:25
-
-
Save pncnmnp/037dd82b17f1707aa7f3801c3aed0250 to your computer and use it in GitHub Desktop.
Computer Network practical practise problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAX 100 | |
int main(void) { | |
char data[MAX], output_data[MAX+MAX]; | |
printf("Enter the data: "); | |
scanf("%s", data); | |
int len = strlen(data); | |
int consec = 0; | |
int j = 0; | |
for(int i = 0; i < len; i++) { | |
if (consec == 5 && data[i] == '1') { | |
output_data[j++] = '0'; | |
output_data[j++] = '1'; | |
consec = 0; | |
} | |
else if (data[i] == '0') { | |
output_data[j++] = '0'; | |
consec = 0; | |
} | |
else if (data[i] == '1') { | |
output_data[j++] = '1'; | |
consec += 1; | |
} | |
} | |
printf("Output data is: %s\n", output_data); | |
consec = 0, j = 0; | |
char decode_data[MAX+MAX]; | |
for(int i = 0; i < strlen(output_data); i++) { | |
if (consec == 5 && output_data[i] == '0') { | |
decode_data[j++] = '1'; | |
consec = 0; | |
} | |
else if (output_data[i] == '0') { | |
decode_data[j++] = '0'; | |
consec = 0; | |
} | |
else if (output_data[i] == '1') { | |
decode_data[j++] = '1'; | |
consec += 1; | |
} | |
} | |
printf("Decoded data is: %s\n", decode_data); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAX 100 | |
int main(void) { | |
char data[MAX][MAX]; | |
char data_out[MAX][MAX]; | |
int len_frames = 0; | |
printf("Enter the number of frames: "); | |
scanf("%d", &len_frames); | |
for(int i = 0; i < len_frames; i++) { | |
printf("Enter frame %d: ", i); | |
scanf("%s", data[i]); | |
for(int j = 0; j < strlen(data[i]) + 1; j++) { | |
if(j == 0) | |
data_out[i][j] = strlen(data[i]) + '0'; | |
else | |
data_out[i][j] = data[i][j-1]; | |
} | |
printf("String is: %s\n", data_out[i]); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define MAX 8 | |
int main(void) { | |
int len = 0; | |
printf("Enter the no. of frames of %d size: ", MAX); | |
scanf("%d", &len); | |
int m_len = len; | |
char frames[len][MAX]; | |
for(int i = 0; i < len; i++) { | |
printf("Enter frame %d: ", i); | |
scanf("%s", frames[i]); | |
} | |
char inter[MAX]; | |
for(int i = 0; i < MAX; i++) { | |
inter[i] = frames[0][i]; | |
} | |
for(int i = 1; i < m_len; i++) { | |
printf("%s\n", inter); | |
for(int j = 0; j < MAX; j++) { | |
int frame_int = frames[i][j] - '0'; | |
int inter_int = inter[j] - '0'; | |
int new_inter = frame_int ^ inter_int; | |
inter[j] = new_inter + '0'; | |
} | |
printf("%s\n", inter); | |
} | |
for(int i = 0; i < MAX; i++) { | |
if (inter[i] == '0') | |
inter[i] = '1'; | |
else | |
inter[i] = '0'; | |
} | |
printf("%s\n", inter); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAX 100 | |
int main(void) { | |
char gen[MAX], data[MAX]; | |
printf("Enter the generator: "); | |
scanf("%s", gen); | |
printf("Enter data: "); | |
scanf("%s", data); | |
char mod_data[strlen(data)+strlen(gen)-1]; | |
for(int i = 0; i < strlen(gen)+strlen(data)-1; i++) { | |
if (i < strlen(data)) | |
mod_data[i] = data[i]; | |
else | |
mod_data[i] = '0'; | |
} | |
char prev[strlen(gen)]; | |
for(int i = 0; i < strlen(gen); i++) { | |
prev[i] = mod_data[i]; | |
} | |
printf("SENDER\n"); | |
for(int i = 0; i < strlen(data); i++) { | |
for(int j = 0; j < strlen(gen); j++) { | |
int curr_j = gen[j] - '0'; | |
int curr_prev = prev[j] - '0'; | |
int curr_res = curr_j ^ curr_prev; | |
prev[j] = curr_res + '0'; | |
} | |
for(int j = 0; j < strlen(gen); j++) { | |
if (j < strlen(gen) - 1) | |
prev[j] = prev[j+1]; | |
else { | |
prev[j] = mod_data[i + strlen(gen)]; | |
} | |
} | |
printf("%s\n", prev); | |
} | |
printf("RECEIVER\n"); | |
int x = 0; | |
for(int i = 0; i < strlen(data) + strlen(prev); i++) { | |
if(i < strlen(data)) | |
mod_data[i] = data[i]; | |
else { | |
mod_data[i] = prev[x]; | |
x+=1; | |
} | |
} | |
for(int i = 0; i < strlen(gen); i++) { | |
prev[i] = mod_data[i]; | |
} | |
for(int i = 0; i < strlen(data)+1; i++) { | |
printf("%s\n", prev); | |
for(int j = 0; j < strlen(gen); j++) { | |
int curr_j = gen[j] - '0'; | |
int curr_prev = prev[j] - '0'; | |
int curr_res = curr_j ^ curr_prev; | |
prev[j] = curr_res + '0'; | |
} | |
for(int j = 0; j < strlen(gen); j++) { | |
if (j < strlen(gen) - 1) | |
prev[j] = prev[j+1]; | |
else { | |
prev[j] = mod_data[i + strlen(gen)]; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment