Search code examples
carraysstringdynamic-memory-allocation

Separating capital letters from char array into the new array and rellocating memory for it in C


I have array in which I enter number of elements, enter all the elements, print them, and after that I should transfer all the capital letters into the new array, and small letters into another array, allocate memory for them and print it. I've tried it this way but it didn't worked:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, i, j, counter=0, counter1=0;
    char *arrA, *arrB, *arrC;

    printf("Enter number of elements: ");
    scanf("%d", &n);
    printf("Enter elements of array: \n");


    arrA = (char*)malloc(n * sizeof(char));

    for (i = 0; i < n; i++) {
        scanf(" %c", &arrA[i]);
    }

    for (i = 0; i < n; i++) {
        printf("%d. element of array is: %c\n", i + 1, arrA[i]);
    }
    arrB = (char*)malloc(counter * sizeof(char));
    arrC = (char*)malloc(counter1 * sizeof(char));

    for (i = 0; i < n; i++) {
        if ((arrA[i] >= 'A') && (arrA[i] <= 'Z')) {
            counter++;
            *arrB = *arrA;
        }
    }
    for (i = 0; i < n; i++) {
        if ((arrA[i] >= 'a') && (arrA[i] <= 'z')) {
            counter1++;
            *arrC = *arrA;
        }
    }


    for (i = 0; i < counter; i++) {
        printf("%d. element of array B is: %c\n", i + 1, arrB[i]);
    }
    for (i = 0; i < counter1; i++) {
        printf("%d. element of array C is: %c\n", i + 1, arrC[i]);
    }
    free(arrA);
    free(arrB);
    free(arrC);

    return 0;
}

How can I separate those capital letters into the new array "arrB"?


Solution

  • You could follow two different approaches.

    The first approach would be to allocate the maximum amount of chars (i.e. arrB = malloc(n * sizeof(char))) for both arrB and arrC. You could then live with some unused memory or realloc these arrays once you have copied the characters and finally know their number (i.e. arrB = realloc(arrB, countCapital * sizeof(char))):

    #include <stdio.h>
    #include <stdlib.h>
    
    int isCapitalLetter(char c) {
        return (c >= 'A') && (c <= 'Z');
    }
    
    int isSmallLetter(char c) {
        return (c >= 'a') && (c <= 'z');
    }
    
    int main() {
        int n=0, i, countCapital, countSmall;
        char *arrA, *arrB, *arrC;
    
    
        printf("Enter number of elements: ");
        scanf("%d", &n);
        printf("Enter elements of array: \n");
    
    
        arrA = malloc(n * sizeof(char));
        arrB = malloc(n * sizeof(char));
        arrC = malloc(n * sizeof(char));
    
        for (i = 0; i < n; i++) {
            scanf(" %c", &arrA[i]);
        }
    
        for (i = 0; i < n; i++) {
            printf("%d. element of array is: %c\n", i + 1, arrA[i]);
        }
    
        countCapital = 0;
        countSmall = 0;
        for (i = 0; i < n; i++) {
            char c = arrA[i];
            if (isCapitalLetter(c)) {
                arrB[countCapital++] = c;
            }
            else if (isSmallLetter(c)) {
                arrC[countSmall++] = c;
            }
        }
        arrB = realloc(arrB, countCapital * sizeof(char));
        arrC = realloc(arrC, countSmall * sizeof(char));
    
        for (i = 0; i < countCapital; i++) {
            printf("%d. element of array B is: %c\n", i + 1, arrB[i]);
        }
        for (i = 0; i < countSmall; i++) {
            printf("%d. element of array C is: %c\n", i + 1, arrC[i]);
        }
        free(arrA);
        free(arrB);
        free(arrC);
    
        return 0;
    }
    

    The second approach would be to count capital and small letters, respectively; then allocate arrB and arrC accordingly before copying the values into them:

    #include <stdio.h>
    #include <stdlib.h>
    
    int isCapitalLetter(char c) {
        return (c >= 'A') && (c <= 'Z');
    }
    
    int isSmallLetter(char c) {
        return (c >= 'a') && (c <= 'z');
    }
    
    int main() {
        int n=0, i, countCapital, countSmall;
        char *arrA, *arrB, *arrC;
    
    
        printf("Enter number of elements: ");
        scanf("%d", &n);
        printf("Enter elements of array: \n");
    
    
        arrA = malloc(n * sizeof(char));
    
        for (i = 0; i < n; i++) {
            scanf(" %c", &arrA[i]);
        }
    
        for (i = 0; i < n; i++) {
            printf("%d. element of array is: %c\n", i + 1, arrA[i]);
        }
    
        countCapital = 0;
        countSmall = 0;
        for (i = 0; i < n; i++) {
            if (isCapitalLetter(arrA[i])) {
                countCapital++;
            }
            else if (isSmallLetter(arrA[i])) {
                countSmall++;
            }
        }
    
        arrB = malloc(countCapital * sizeof(char));
        arrC = malloc(countSmall * sizeof(char));
    
        char *writeCaptialPtr = arrB;
        char *writeSmallPtr = arrC;
        for (i = 0; i < n; i++) {
            char c = arrA[i];
            if (isCapitalLetter(c)) {
                *writeCaptialPtr++ = c;
            }
            else if (isSmallLetter(c)) {
                *writeSmallPtr++ = c;
            }
        }
    
        for (i = 0; i < countCapital; i++) {
            printf("%d. element of array B is: %c\n", i + 1, arrB[i]);
        }
        for (i = 0; i < countSmall; i++) {
            printf("%d. element of array C is: %c\n", i + 1, arrC[i]);
        }
        free(arrA);
        free(arrB);
        free(arrC);
    
        return 0;
    }