c

size of array is calculated weirdly in very specific cases


I have this code that takes parameters from a user input and it is supposed to combine each give parameter into a single string with spaces in between. The combined string cant be longer than 10 characters tho. If the given parameters have more than 10 characters, the parameter that exceeds the 10 character threshhold is cut in a way that the combined string just takes enough characters so that it reaches 10 characters. For example if the user puts the parameters "hello" and "world" in, the programm is supposed to combine them into a single string saying "hello worl" since the space in between also counts as character. The programm works in any scenario except for when the first given parameter (argv[1]) is a 5 letter word? In that case the combined string will cut 1 character too early. So if I put in "hello" and world" it gives me "hello wor" instead of the expected "hello worl". If i put in "hi and welcome", it correctly outputs "hi and wel". I have no idea why that is.

For debugging purposes i tried printing out the size calculated by the getSize() function and for some reason it thinks the string "hello " has a size of 7 although its only 6 characters. Funny thing is that it also correctly says that the string "helloo " is 7 characters long.

This is the part of the code:

int getSize(char list[])
{
    int size = 0;
    while (list[size] != '\0') {
        size++;
    }

    return size;
}

/*Part of main function*/
int i;

for(i = 1; i < argc; i++) {
    int argSize = getSize(argv[i]);

    if(getSize(combined_text) + argSize <= 10) {
        strcat(combined_text, argv[i]);
        if(getSize(combined_text) != 10) {
            combined_text[getSize(combined_text)] = '%';
        } else break;
    } else {
         int diff = 10 - getSize(combined_text);

         strncat(combined_text, argv[i], diff);
         break;
    }
}
printf("\n%s", combined_text);

Solution

  • The main issues with your  method were that when strings were concatenated, undefined behaviour resulted from the failure to initialise combined_text. Unexpected outcomes resulted from the program's improper addition of the % character, even when the total string length reached exactly 10 characters. Below is the correct code.

    Note: I checked with the online complier that is why I took user input you can use it by command line argument.

    #include <stdio.h>
    #include <string.h>
    
    int getSize(char list[]) {
        int size = 0;
        while (list[size] != '\0') {
            size++;
        }
        return size;
    }
    
    int main() {
        char combined_text[11] = "";
        char input[100];
        char *word;
        int maxLength = 10;
    
        printf("Enter words separated by spaces:\n");
        fgets(input, sizeof(input), stdin);
    
        word = strtok(input, " \n");
    
        while (word != NULL) {
            int argSize = getSize(word);
    
            if (getSize(combined_text) + argSize <= maxLength) {
                strcat(combined_text, word);
                if (getSize(combined_text) < maxLength) {
                    strcat(combined_text, " ");
                }
            } else {
                int diff = maxLength - getSize(combined_text);
                strncat(combined_text, word, diff);
                break;
            }
    
            word = strtok(NULL, " \n");
        }
    
        int len = getSize(combined_text);
        if (len > 0 && combined_text[len - 1] == ' ') {
            combined_text[len - 1] = '\0';
        }
    
        printf("\nCombined Text: %s\n", combined_text);
        return 0;
    }