Thread: Joining 2 2D arrays

  1. #1
    Registered User
    Join Date
    Jun 2022
    Posts
    5

    Joining 2 2D arrays

    Hey, my function traverses a 2D array of a sentence and a 2D array of words. The function then compares every position that word/s is in the sentence and replaces it with asterisks based on the length of the word. However, I want to store the new sentence in a new array and return that array. So far I've been able to do the comparison but applying the asterisks and creating the new array has been the trouble.

    Code:
    char *censor(char *message, char *list)
    {
        int sentencelen, wordlen,i,j,k,r;
        
        //words are split
        char** new_sentence = split_message(message, &sentencelen);
        char** new_list = split_message(list, &wordlen);
        
        //allocating memory for new array
        char *result = malloc(sizeof(char)*strlen(message));
    
        
        for(i=0;i<sentencelen;i++)
        {
            r=0;
            for(j=0;j<wordlen;j++)
            {
                if(strcasecmp(new_list[j],new_sentence[i])==0)
                {
                    r++;//increments when the word is found
                }
                //need help here
                if(r==1)//if word is found
                {
                    for(k=0;k<strlen(new_list[j]);k++)
                    {
                        //the lenght of the word replace with "*"
                    }
                }
                else
                {
                    //attach the other words
                }
    
    
            }
        }
    Last edited by Salem; 06-12-2022 at 02:29 AM. Reason: font/size abuse

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,154
    It would be helpful to see an example of a before and after.

    > char *censor(char *message, char *list)
    What is the output of
    censor("the cat sat on the mat","at cat mat");

    message and list are not exactly meaningful names based on your description:
    > my function traverses a 2D array of a sentence and a 2D array of words.

    So perhaps open with
    Code:
    char *censor(char *sentence, char *words) {
        //words are split
        char** split_sentence = split_message(sentence, &sentencelen);
        char** split_words = split_message(words, &wordlen);
    }
    If your result is something like "the *** s** on the ***", then you need
    char *result = malloc(sizeof(char)*strlen(message)+1);
    You need +1 to store the \0 at the end of the string.


    Code:
        *result = '\0'; // start with an empty string
        for(int i=0;i<sentencelen;i++)
        {
            bool found = false;
            for(int j=0;j<wordlen && !found;j++)
            {
                if(strcasecmp(split_words[j],split_sentence[i])==0)
                {
                    found = true;
                }
            }
            if(found) {
                size_t result_len = strlen(result);
                size_t word_len = strlen(split_sentence[i]);
                for ( size_t n = 0 ; n < word_len ; n++ )
                    result[result_len++] = '*';
                result[result_len] = '\0';
            } else {
                strcat(result,split_sentence[i]);
            }
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,387
    Salem forgot some parentheses, showing how easy it is to do and the weakness of the malloc interface. Although it happens to make no difference in this particular case, it should be:
    Code:
    char *result = malloc(sizeof(char) * (strlen(message) + 1));
    It makes no difference in this case since sizeof(char) is always 1 by definition. If you were malloc'ing ints (or anything that usually has a sizeof greater than 1), it would make a difference.

    calloc fixes the weak malloc/realloc interfaces by accepting the number of elements and the size of each element as separate parameters so it's not possible to forget the parentheses. This suggests that calloc was added to C after malloc and realloc (and many many forgotten parentheses).
    A long time ago being crazy meant something. - Charles Manson

  4. #4
    Registered User
    Join Date
    Jun 2022
    Posts
    5
    The output seems to spit out random characters at the beginning of the new sentence.

    Sentence input: The quick brown fox jumps jump-suit over the lazy dog;
    Words to be censored: the,lazy,jumps;
    Output: @iÓ***quickbrownfox*****jump-suitover*******dog.

    The censoring works pretty great. I assume the random characters is a result of the memory being allocated not being filled since the strlen(message)=53(spaces being count) and I found the strlen(result)=47. How can I add spaces between each word to perhaps compensate for the extra memory that was allocated. (I am new to dynamic allocation of memory so that is my best guess).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,154
    Garbage at the start of the string is a sign that you're not initialising your dynamically allocated string properly.

    See my example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,387
    What about the spacing between words?
    And what do you do with punctuation?
    I assume if the input sentence is:

    Here is a dirty sentence using rotten words, a sentence with dirty punctuation.

    And the word list is:

    dirty
    rotten

    Then the output should be:

    Here is a ***** sentence using ****** words, a sentence with ***** punctuation.
    A long time ago being crazy meant something. - Charles Manson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threading and joining
    By baxy in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2015, 06:56 AM
  2. joining codes
    By smk3000 in forum C Programming
    Replies: 5
    Last Post: 12-09-2012, 09:31 AM
  3. Joining strings.
    By DaveHope in forum C++ Programming
    Replies: 10
    Last Post: 06-21-2005, 08:15 AM
  4. Strings joining
    By Lord CyKill in forum C++ Programming
    Replies: 1
    Last Post: 04-13-2003, 05:02 AM
  5. Joining arrays?
    By C-Struggler in forum C Programming
    Replies: 3
    Last Post: 04-07-2003, 07:55 PM

Tags for this Thread