Question

The legal department of a big company is looking for a way to redact certain sensitive...

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.

$ cat udhr_art26.txt
(1) Everyone has the right to education. Education shall be
free, at least in the elementary and fundamental stages.
Elementary education shall be compulsory.  Technical and
professional education shall be made generally available and
higher education shall be equally accessible to all on the
basis of merit.
(2) Education shall be directed to the full development of
the human personality and to the strengthening of respect
for human rights and fundamental freedoms.  It shall promote
understanding, tolerance and friendship among all nations,
racial or religious groups, and shall further the activities
of the United Nations for the maintenance of peace.
(3) Parents have a prior right to choose the kind of
education that shall be given to their children.
$ ./redact education right t < udhr_art26.txt
(1) Everyone has the r**** to e********. E******** shall be
free, at least in the elementary and fundamental stages.
Elementary e******** shall be compulsory.  Technical and
professional e******** shall be made generally available and
higher e******** shall be equally accessible to all on the
basis of merit.
(2) E******** shall be directed to the full development of
the human personality and to the strengthening of respect
for human r****s and fundamental freedoms.  It shall promote
understanding, tolerance and friendship among all nations,
racial or religious groups, and shall further the activities
of the United Nations for the maintenance of peace.
(3) Parents have a prior r**** to choose the kind of
e******** that shall be given to their children.
$

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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] == '\0')//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\n", 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
    }
}

Add a comment
Know the answer?
Add Answer to:
The legal department of a big company is looking for a way to redact certain sensitive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

  • Read “Instituionalizing our Demise: America vs Multiculturalism” by Roger Kimball on pg 268 and “Reinventing America”...

    Read “Instituionalizing our Demise: America vs Multiculturalism” by Roger Kimball on pg 268 and “Reinventing America” Call for a new national indentity” by Elizabeth Martinez on pg 275. Create a double entry notebook for each reading selection It should be atleast five observation and responses. wric 268 PART 2 essay pro. exactly how and why their authors disagree. Instead of with parties in conflict as mediators do, you will nt of view designed to appeal to both sides, mediatn posing...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT