3. 1. 1 Redact The 1egal department of a big company is looking for a way to red
ID: 3726973 • Letter: 3
Question
3. 1. 1 Redact The 1egal department of a big company is looking for a way to redact certain sensitive documents before they can be released publicly. A redacted document has certain words deleted or blacked out. For this problem, censored words will be hidden with asterisks, leaving only their first letter intact. Censored words should be matched regardless of their case. Write program redact. c that reads entire lines from the user, and hides certain words according to a list of words received as program arguments, as illustrated in the following example.Explanation / Answer
ANSWER:
THE FOLLOWING CODE SHOWS THE CODE FOR THE GIVEN DATA:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
//
///*
// *
// */
//function to split the sentence read from file into words
int split(char str, char arr[20]) {
int beginIndex = 0;
int endIndex;
int maxWords = 20;//Assuming Maximum words in a line can be 20
int wordCnt = 0;
while (1) {
while (isspace(str[beginIndex])) {//check if the character is a space
++beginIndex;
}
if (str[beginIndex] == '')//if it is a space then this is end of word, add null character
break;
endIndex = beginIndex;
while (str[endIndex] && !isspace(str[endIndex])) {//find the starting and eding index of the word
++endIndex;
}
int len = endIndex - beginIndex;
char *tmp = calloc(len + 1, sizeof (char));
memcpy(tmp, &str[beginIndex], len);//get the word
arr[wordCnt++] = tmp;//store the word in array
beginIndex = endIndex;
if (wordCnt == maxWords)
break;
}
return wordCnt;
}
void censorLine(char line, int count, char *words) {
int i, j, k;
char *arr[20]; //Maybe 20 words in a line
int n = split(line, arr);
for (i = 0; i < n; i++) {
for (j = 0; j < count; j++) {
if (strncasecmp(arr[i], words[j], strlen(words[j])) == 0)
for (k = 1; k < strlen(arr[i]); k++)
arr[i][k] = '*'; //replace characters with * except the first character
}
printf("%s ", arr[i]);
}
}
int main(int argc, char** argv) {
FILE *fin;
fin = fopen("udhr_art26.txt", "r");
int i;
for (i = 0; i < argc; i++) {
printf("%s ", argv[i]);
}
char* line = malloc(80); //read 1 line and allocate 80 because 1 line can have 80 characters
while (fgets(line, 80, fin) != NULL) {//read data from file line by line
censorLine(line, argc, argv); //censor line to the user
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.