Untitled

 avatar
unknown
c_cpp
2 months ago
1.8 kB
7
Indexable
#include <stdio.h>

int connect(const char* t1, const char* t2, const char* t3, char* t4, int size);
void trim(char *str);

int main() {
    char t1[1001] = {0};
    char t2[1001] = {0};
    char t3[1001] = {0};
    char output[3004];
    
    printf("Podaj tekst:\n");
    scanf("%1000[^\n]", t1);
    while (getchar() != '\n');
    
    printf("Podaj tekst:\n");
    scanf("%1000[^\n]", t2);
    while (getchar() != '\n');
    
    printf("Podaj tekst:\n");
    scanf("%1000[^\n]", t3);
    while (getchar() != '\n');
    
    if (connect(t1, t2, t3, output, sizeof(output)) == 0) {
        trim(output);
        printf("%s\n", output);
    }
    
    return 0;
}

int connect(const char* t1, const char* t2, const char* t3, char* t4, int size) {
    int len1 = 0, len2 = 0, len3 = 0;
    const char *p = t1;
    while (*p) { len1++; p++; }
    p = t2;
    while (*p) { len2++; p++; }
    p = t3;
    while (*p) { len3++; p++; }
    
    int required = len1 + len2 + len3 + 2 + 1;
    if (required > size) return 1;
    
    while (*t1) *t4++ = *t1++;
    *t4++ = ' ';
    
    while (*t2) *t4++ = *t2++;
    *t4++ = ' ';
    
    while (*t3) *t4++ = *t3++;
    *t4 = '\0';
    
    return 0;
}

void trim(char *str) {
    if (*str == '\0') return;
    
    char *end = str;
    while (*end) end++;
    end--;
    
    while (end >= str && (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r' || *end == '\v' || *end == '\f'))
        end--;
    *(end + 1) = '\0';
    
    char *start = str;
    while (*start == ' ' || *start == '\t' || *start == '\n' || *start == '\r' || *start == '\v' || *start == '\f')
        start++;
    
    if (start != str) {
        char *dst = str;
        while (*start)
            *dst++ = *start++;
        *dst = '\0';
    }
}
Editor is loading...
Leave a Comment