לדלג לתוכן

4.2 מחרוזות פתרון

פתרונות

פתרון 1:

char* clean_and_concat(char *a, char *b) {
    char cleaned[100];
    int idx = 0;
    for (int i = 0; a[i] != '\0'; i++) {
        if (a[i] != ' ' && a[i] != '\n') {
            cleaned[idx++] = a[i];
        }
    }
    cleaned[idx] = '\0';

    char *result = malloc(strlen(cleaned) + strlen(b) + 1);
    strcpy(result, cleaned);
    strcat(result, b);
    return result;
}

פתרון 2:

char buffer[64];
memset(buffer, '-', sizeof(buffer));
memcpy(buffer, "START", strlen("START") + 1); // כולל תו סיום