The legal department of a big company is looking for a way to redact certain sen
ID: 3725519 • Letter: T
Question
The legal 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.
Here are a list of requirements, assumptions and hints:
This program shall contain no global variables.
We assume that the maximum number of characters a line can contain is 80.
Censored words less than 2 characters should be ignored.
You will probably need to split the problem into a hierarchy of functions. A possible set of functions could include:
A function that hides a certain number of characters in a line, starting from a given position.
A function that hides a certain word in a line (e.g. by iterating through the string and locating the word).
A function that hides a collection of words in a line.
The main function should be the brain, reading the line from the user and calling the censor function before displaying the censored line.
In order to match strings while ignoring their case, you can use strncasecmp()function provided by the libc.
List of some important libc functions that are used in the reference program: strlen(), strncasecmp(), fgets().
follot the guidelines if possible
Explanation / Answer
Please read the comments to understand the functionality of the code.
#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.