-1

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Nox5692
  • 150
  • 8
  • Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Dec 03 '22 at 19:09
  • Please note that [it is generally expected that you make a debugging attempt yourself before asking for help on Stack Overflow](https://idownvotedbecau.se/nodebugging/). Questions which do not demonstrate any debugging attempt and do not specify what you have learnt in the debugging attempt, are usually not well received. – Andreas Wenzel Dec 03 '22 at 19:09
  • I tried runnig it through pythontutor.com, is there a debugger that you would recommend for macOS M1? – Nox5692 Dec 03 '22 at 19:11
  • Are you using an [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment), such as [XCode](https://en.wikipedia.org/wiki/Xcode)? Most IDEs (including XCode) have either a built-in debugger or at least offer some way to install a debugger as an add-on. You may want to read this: [Why is debugging better in an IDE?](https://stackoverflow.com/q/426569/12149471) – Andreas Wenzel Dec 03 '22 at 19:14

1 Answers1

0

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256