Home > OS >  Replacing words in a string with words given in a 2D array
Replacing words in a string with words given in a 2D array

Time:12-06

I'm currently working on a program that corrects given words in a sentence to be more polite.

I'm building a function that is given the original sentence and a 2D array, that stores the words we should look for and the ones we will replace them with.
This is my main function where the "dictionary" is declared:


int main(){
    const char * d1 [][2] =
    {
        { "hey", "hello" },
        { "bro", "sir" },
        { NULL, NULL }
    };
    printf("%s\n", newSpeak("well hey bro", d1) );

}

This functions job is to go through every pointer of the original string and check it with the first character of each word, that could potentially be 'bad'. If it catches the first letter, then it will go through the rest of the word and if it goes all the way to the end of the word, it will skip the original word and replace it with the 'good' word.
This is the function itself:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>

char * newSpeak ( const char * text, const char * (*replace)[2] )
{   
    char * result = (char*)malloc( sizeof(char) );
    int resIndex = 0; // Pointer to final text
    int matches = 0; // 1 - Matches word from library, 0 - Does not
    // Run through the whole original text
    for ( int index = 0; text[index] != '\0'; index   ){

        for ( int line = 0; replace[line][0] != NULL; line   ){
            // If the first letter of the word matches, do the others match too?
            // If yes, don't rewrite the original word, skip it, and write the replacement one by one.
            if ( replace[line][0][0] == text[index] ){
                matches = 1;
                // Check one by one if letters from the word align with letters in the original string
                for ( int letter = 0; replace[line][0][letter] != '\0'; letter   ){
                    if ( replace[line][0][letter] != text[index   letter] ){
                        matches = 0;
                        break;
                    }
                }
                // If the whole word matches, skip what would be copied from original text (the bad word) and put in replacement letter by letter
                if ( matches == 1 ){
                    // Push pointer of original string after the word
                    index  = strlen( replace[line][0] );
                    for ( int r = 0; r < strlen( replace[line][1] ); r  ){
                        result = (char*)realloc(result, (strlen( result )   1) * sizeof(char));
                        result[resIndex   r] = replace[line][1][r];
                        index  = r; 
                    }
                }
            }
        }
        if ( matches == 0 ){
            result = (char*)realloc(result, (strlen( result )   1) * sizeof(char)); 
            result[resIndex] = text[index];
        }
        resIndex  ;
    }
    return result;
}

After this is run, my expected outcome is well hello sir, but instead, the function only returns well hello.

I am looking for an explanation to why the loop would stop and not check for the rest of the string, any help would be appreciated.

CodePudding user response:

At least this problem:

strlen( result ) in result = (char*)realloc(result, (strlen( result ) 1) * sizeof(char)); is not valid as result does not point to a string. Missing null character.

  • Related