c

exercises

exercises.c🔧
/**
 * =============================================================================
 * POINTERS AND ARRAYS - EXERCISES
 * =============================================================================
 * 
 * This file contains hands-on exercises to reinforce the relationship between
 * pointers and arrays in C.
 * 
 * Difficulty Levels:
 *   ⭐         - Beginner
 *   ⭐⭐       - Elementary
 *   ⭐⭐⭐     - Intermediate
 *   ⭐⭐⭐⭐   - Advanced
 *   ⭐⭐⭐⭐⭐ - Expert
 * 
 * Instructions:
 *   1. Each exercise has a TODO section for you to complete
 *   2. Expected outputs are provided for verification
 *   3. Solutions are at the end of the file
 *   4. Test your solutions by uncommenting the main function
 * 
 * Compile: gcc -o exercises exercises.c -Wall
 * Run: ./exercises
 * =============================================================================
 */

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


/* =============================================================================
 * EXERCISE 1: Array Name as Pointer (⭐)
 * =============================================================================
 * 
 * Demonstrate that array name is a constant pointer to first element.
 * 
 * Tasks:
 *   1. Print the address of the array using array name
 *   2. Print the address of first element using & operator
 *   3. Print the first element using dereferencing
 *   4. Show that all addresses are the same
 * 
 * Expected Output:
 *   Array address (arr): <address>
 *   First element address (&arr[0]): <same address>
 *   First element using *arr: 100
 *   First element using arr[0]: 100
 */

void exercise1_array_as_pointer(void) {
    int arr[5] = {100, 200, 300, 400, 500};
    
    // TODO: Complete the exercise
    // 1. Print address of arr
    // 2. Print address of &arr[0]
    // 3. Print *arr
    // 4. Print arr[0]
    
    printf("=== EXERCISE 1: Array Name as Pointer ===\n\n");
    
    // Your code here...
    
}


/* =============================================================================
 * EXERCISE 2: Pointer Arithmetic Basics (⭐)
 * =============================================================================
 * 
 * Use pointer arithmetic to access array elements.
 * 
 * Tasks:
 *   1. Create a pointer to the array
 *   2. Access elements using ptr + offset
 *   3. Calculate address difference between consecutive elements
 * 
 * Expected Output:
 *   Element at ptr+0: 10
 *   Element at ptr+1: 20
 *   Element at ptr+2: 30
 *   Element at ptr+3: 40
 *   Element at ptr+4: 50
 *   Address step size: 4 bytes
 */

void exercise2_pointer_arithmetic(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    
    // TODO: Create pointer and use arithmetic to access elements
    
    printf("=== EXERCISE 2: Pointer Arithmetic Basics ===\n\n");
    
    // Your code here...
    
}


/* =============================================================================
 * EXERCISE 3: Four Access Methods (⭐⭐)
 * =============================================================================
 * 
 * Demonstrate all four ways to access array elements.
 * 
 * Tasks:
 *   Access arr[3] (value 40) using all four methods:
 *   1. Array indexing: arr[3]
 *   2. Pointer arithmetic on array: *(arr + 3)
 *   3. Pointer indexing: ptr[3]
 *   4. Pointer arithmetic on pointer: *(ptr + 3)
 * 
 * Expected Output:
 *   Method 1 (arr[3]): 40
 *   Method 2 (*(arr+3)): 40
 *   Method 3 (ptr[3]): 40
 *   Method 4 (*(ptr+3)): 40
 */

void exercise3_access_methods(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    int *ptr = arr;
    
    // TODO: Access arr[3] using all four methods
    
    printf("=== EXERCISE 3: Four Access Methods ===\n\n");
    
    // Your code here...
    
}


/* =============================================================================
 * EXERCISE 4: Traverse Array with Pointer (⭐⭐)
 * =============================================================================
 * 
 * Write a function that prints array elements using a pointer.
 * 
 * Tasks:
 *   1. Use pointer increment (ptr++) to traverse
 *   2. Print each element as you traverse
 *   3. Do NOT use array indexing
 * 
 * Expected Output:
 *   Elements: 5 10 15 20 25 30
 */

void exercise4_traverse_with_pointer(void) {
    int arr[6] = {5, 10, 15, 20, 25, 30};
    int size = 6;
    
    // TODO: Traverse using pointer only, no arr[i]
    
    printf("=== EXERCISE 4: Traverse with Pointer ===\n\n");
    printf("Elements: ");
    
    // Your code here...
    
    printf("\n");
}


/* =============================================================================
 * EXERCISE 5: Sum Array Using Pointers (⭐⭐)
 * =============================================================================
 * 
 * Calculate sum of array elements using pointer arithmetic.
 * 
 * Tasks:
 *   1. Use a pointer to traverse the array
 *   2. Accumulate sum without using array indexing
 * 
 * Expected Output:
 *   Sum of elements: 150
 */

void exercise5_sum_with_pointers(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    int size = 5;
    int sum = 0;
    
    // TODO: Calculate sum using pointers
    
    printf("=== EXERCISE 5: Sum Using Pointers ===\n\n");
    
    // Your code here...
    
    printf("Sum of elements: %d\n", sum);
}


/* =============================================================================
 * EXERCISE 6: Reverse Array Using Pointers (⭐⭐⭐)
 * =============================================================================
 * 
 * Reverse an array in-place using two pointers.
 * 
 * Tasks:
 *   1. Create pointers to start and end
 *   2. Swap elements using pointers
 *   3. Move pointers towards center
 * 
 * Expected Output:
 *   Original: 1 2 3 4 5
 *   Reversed: 5 4 3 2 1
 */

void exercise6_reverse_array(void) {
    int arr[5] = {1, 2, 3, 4, 5};
    int size = 5;
    
    printf("=== EXERCISE 6: Reverse Array ===\n\n");
    
    printf("Original: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    
    // TODO: Reverse using two pointers (start and end)
    
    // Your code here...
    
    printf("Reversed: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}


/* =============================================================================
 * EXERCISE 7: Find Maximum Using Pointers (⭐⭐⭐)
 * =============================================================================
 * 
 * Find the maximum element and its position using pointers.
 * 
 * Tasks:
 *   1. Use a pointer to traverse
 *   2. Keep track of max value and position
 *   3. Return position using pointer arithmetic (ptr - arr)
 * 
 * Expected Output:
 *   Maximum value: 90
 *   Position: 4
 */

void exercise7_find_maximum(void) {
    int arr[7] = {23, 45, 67, 12, 90, 34, 56};
    int size = 7;
    
    printf("=== EXERCISE 7: Find Maximum ===\n\n");
    
    // TODO: Find max and its position using pointers
    
    // Your code here...
    
}


/* =============================================================================
 * EXERCISE 8: Copy Array Using Pointers (⭐⭐⭐)
 * =============================================================================
 * 
 * Copy contents of one array to another using pointers.
 * 
 * Tasks:
 *   1. Create pointers to source and destination
 *   2. Copy element by element using dereferencing
 *   3. Do NOT use array indexing
 * 
 * Expected Output:
 *   Source: 10 20 30 40 50
 *   Destination: 10 20 30 40 50
 */

void exercise8_copy_array(void) {
    int source[5] = {10, 20, 30, 40, 50};
    int dest[5];
    int size = 5;
    
    printf("=== EXERCISE 8: Copy Array ===\n\n");
    
    // TODO: Copy source to dest using pointers
    
    // Your code here...
    
    printf("Source: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", source[i]);
    }
    printf("\n");
    
    printf("Destination: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", dest[i]);
    }
    printf("\n");
}


/* =============================================================================
 * EXERCISE 9: Bubble Sort with Pointers (⭐⭐⭐⭐)
 * =============================================================================
 * 
 * Implement bubble sort using only pointers.
 * 
 * Tasks:
 *   1. Use pointers instead of array indexing
 *   2. Swap elements using pointer dereferencing
 *   3. Sort in ascending order
 * 
 * Expected Output:
 *   Before: 64 34 25 12 22 11 90
 *   After:  11 12 22 25 34 64 90
 */

void exercise9_bubble_sort(void) {
    int arr[7] = {64, 34, 25, 12, 22, 11, 90};
    int size = 7;
    
    printf("=== EXERCISE 9: Bubble Sort with Pointers ===\n\n");
    
    printf("Before: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    
    // TODO: Implement bubble sort using pointers only
    
    // Your code here...
    
    printf("After:  ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}


/* =============================================================================
 * EXERCISE 10: Pointer Difference (⭐⭐⭐)
 * =============================================================================
 * 
 * Calculate and demonstrate pointer difference.
 * 
 * Tasks:
 *   1. Create two pointers pointing to different array elements
 *   2. Calculate the number of elements between them
 *   3. Show both element count and byte difference
 * 
 * Expected Output:
 *   ptr1 points to index 1 (value: 20)
 *   ptr2 points to index 4 (value: 50)
 *   Element difference: 3
 *   Byte difference: 12
 */

void exercise10_pointer_difference(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    
    printf("=== EXERCISE 10: Pointer Difference ===\n\n");
    
    // TODO: Create pointers to arr[1] and arr[4], calculate difference
    
    // Your code here...
    
}


/* =============================================================================
 * EXERCISE 11: 2D Array Access with Pointers (⭐⭐⭐⭐)
 * =============================================================================
 * 
 * Access 2D array elements using pointers.
 * 
 * Tasks:
 *   1. Access specific elements using *(*(arr + i) + j)
 *   2. Print the entire 2D array using pointers
 *   3. Calculate and print address of each element
 * 
 * Expected Output:
 *   matrix[1][2] = 6
 *   Full matrix:
 *   1 2 3
 *   4 5 6
 *   7 8 9
 */

void exercise11_2d_array_pointers(void) {
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    printf("=== EXERCISE 11: 2D Array with Pointers ===\n\n");
    
    // TODO: Access matrix[1][2] using pointer notation
    // TODO: Print entire matrix using pointer notation
    
    // Your code here...
    
}


/* =============================================================================
 * EXERCISE 12: Array Comparison Using Pointers (⭐⭐⭐)
 * =============================================================================
 * 
 * Compare two arrays for equality using pointers.
 * 
 * Tasks:
 *   1. Compare arrays element by element using pointers
 *   2. Return 1 if equal, 0 if not equal
 *   3. Print which index differs if not equal
 * 
 * Expected Output:
 *   Comparing arr1 and arr2: EQUAL
 *   Comparing arr1 and arr3: NOT EQUAL (differs at index 3)
 */

void exercise12_array_comparison(void) {
    int arr1[5] = {10, 20, 30, 40, 50};
    int arr2[5] = {10, 20, 30, 40, 50};
    int arr3[5] = {10, 20, 30, 99, 50};
    int size = 5;
    
    printf("=== EXERCISE 12: Array Comparison ===\n\n");
    
    // TODO: Compare arrays using pointers
    
    // Your code here...
    
}


/* =============================================================================
 * EXERCISE 13: Merge Two Sorted Arrays (⭐⭐⭐⭐)
 * =============================================================================
 * 
 * Merge two sorted arrays into a third array using pointers.
 * 
 * Tasks:
 *   1. Use pointers to traverse both arrays
 *   2. Compare and merge in sorted order
 *   3. Handle remaining elements
 * 
 * Expected Output:
 *   Array 1: 1 3 5 7
 *   Array 2: 2 4 6 8
 *   Merged:  1 2 3 4 5 6 7 8
 */

void exercise13_merge_arrays(void) {
    int arr1[4] = {1, 3, 5, 7};
    int arr2[4] = {2, 4, 6, 8};
    int merged[8];
    int size1 = 4, size2 = 4;
    
    printf("=== EXERCISE 13: Merge Sorted Arrays ===\n\n");
    
    printf("Array 1: ");
    for (int i = 0; i < size1; i++) printf("%d ", arr1[i]);
    printf("\n");
    
    printf("Array 2: ");
    for (int i = 0; i < size2; i++) printf("%d ", arr2[i]);
    printf("\n");
    
    // TODO: Merge using pointers
    
    // Your code here...
    
    printf("Merged:  ");
    for (int i = 0; i < size1 + size2; i++) {
        printf("%d ", merged[i]);
    }
    printf("\n");
}


/* =============================================================================
 * EXERCISE 14: Linear Search with Pointer Return (⭐⭐⭐)
 * =============================================================================
 * 
 * Search for an element and return pointer to it.
 * 
 * Tasks:
 *   1. Search using pointer traversal
 *   2. Return pointer to found element (or NULL)
 *   3. Use returned pointer to modify the element
 * 
 * Expected Output:
 *   Searching for 30: Found at position 2
 *   Modifying 30 to 300
 *   Array after modification: 10 20 300 40 50
 *   Searching for 99: Not found
 */

int* linear_search(int *arr, int size, int target) {
    // TODO: Implement search, return pointer to found element or NULL
    
    return NULL;  // Replace with your implementation
}

void exercise14_search_with_pointer(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    int size = 5;
    
    printf("=== EXERCISE 14: Search with Pointer Return ===\n\n");
    
    // TODO: Search for 30, modify it to 300
    // TODO: Search for 99 (not found)
    
    // Your code here...
    
}


/* =============================================================================
 * EXERCISE 15: String Operations with Pointers (⭐⭐⭐⭐)
 * =============================================================================
 * 
 * Implement string length and copy using char pointers.
 * 
 * Tasks:
 *   1. Calculate string length using pointer
 *   2. Copy string using pointers
 *   3. Do NOT use strlen() or strcpy()
 * 
 * Expected Output:
 *   Original: "Hello, World!"
 *   Length: 13
 *   Copy: "Hello, World!"
 */

int my_strlen(const char *str) {
    // TODO: Implement strlen using pointers
    
    return 0;  // Replace with your implementation
}

void my_strcpy(char *dest, const char *src) {
    // TODO: Implement strcpy using pointers
    
}

void exercise15_string_operations(void) {
    const char *original = "Hello, World!";
    char copy[50];
    
    printf("=== EXERCISE 15: String Operations ===\n\n");
    
    printf("Original: \"%s\"\n", original);
    
    // TODO: Calculate length and copy string
    
    // Your code here...
    
}


/* =============================================================================
 * MAIN FUNCTION
 * =============================================================================
 * Uncomment the exercises you want to test
 */

int main() {
    printf("╔══════════════════════════════════════════════════════════╗\n");
    printf("║     POINTERS AND ARRAYS - EXERCISE FILE                  ║\n");
    printf("╚══════════════════════════════════════════════════════════╝\n");
    
    // Uncomment exercises to test your solutions:
    
    // exercise1_array_as_pointer();
    // exercise2_pointer_arithmetic();
    // exercise3_access_methods();
    // exercise4_traverse_with_pointer();
    // exercise5_sum_with_pointers();
    // exercise6_reverse_array();
    // exercise7_find_maximum();
    // exercise8_copy_array();
    // exercise9_bubble_sort();
    // exercise10_pointer_difference();
    // exercise11_2d_array_pointers();
    // exercise12_array_comparison();
    // exercise13_merge_arrays();
    // exercise14_search_with_pointer();
    // exercise15_string_operations();
    
    printf("\n[Uncomment exercises in main() to test them]\n");
    
    return 0;
}


/* =============================================================================
 * SOLUTIONS
 * =============================================================================
 * 
 * Scroll down to see solutions...
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * =============================================================================
 */


/* ----- SOLUTION 1: Array Name as Pointer ----- */
void solution1_array_as_pointer(void) {
    int arr[5] = {100, 200, 300, 400, 500};
    
    printf("=== EXERCISE 1: Array Name as Pointer ===\n\n");
    
    printf("Array address (arr): %p\n", (void*)arr);
    printf("First element address (&arr[0]): %p\n", (void*)&arr[0]);
    printf("First element using *arr: %d\n", *arr);
    printf("First element using arr[0]: %d\n", arr[0]);
}


/* ----- SOLUTION 2: Pointer Arithmetic Basics ----- */
void solution2_pointer_arithmetic(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    int *ptr = arr;
    
    printf("=== EXERCISE 2: Pointer Arithmetic Basics ===\n\n");
    
    for (int i = 0; i < 5; i++) {
        printf("Element at ptr+%d: %d\n", i, *(ptr + i));
    }
    
    printf("Address step size: %ld bytes\n", 
           (long)((char*)(ptr + 1) - (char*)ptr));
}


/* ----- SOLUTION 3: Four Access Methods ----- */
void solution3_access_methods(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    int *ptr = arr;
    
    printf("=== EXERCISE 3: Four Access Methods ===\n\n");
    
    printf("Method 1 (arr[3]): %d\n", arr[3]);
    printf("Method 2 (*(arr+3)): %d\n", *(arr + 3));
    printf("Method 3 (ptr[3]): %d\n", ptr[3]);
    printf("Method 4 (*(ptr+3)): %d\n", *(ptr + 3));
}


/* ----- SOLUTION 4: Traverse with Pointer ----- */
void solution4_traverse_with_pointer(void) {
    int arr[6] = {5, 10, 15, 20, 25, 30};
    int size = 6;
    int *ptr = arr;
    int *end = arr + size;
    
    printf("=== EXERCISE 4: Traverse with Pointer ===\n\n");
    printf("Elements: ");
    
    while (ptr < end) {
        printf("%d ", *ptr);
        ptr++;
    }
    printf("\n");
}


/* ----- SOLUTION 5: Sum Using Pointers ----- */
void solution5_sum_with_pointers(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    int size = 5;
    int sum = 0;
    int *ptr = arr;
    int *end = arr + size;
    
    printf("=== EXERCISE 5: Sum Using Pointers ===\n\n");
    
    while (ptr < end) {
        sum += *ptr;
        ptr++;
    }
    
    printf("Sum of elements: %d\n", sum);
}


/* ----- SOLUTION 6: Reverse Array ----- */
void solution6_reverse_array(void) {
    int arr[5] = {1, 2, 3, 4, 5};
    int size = 5;
    
    printf("=== EXERCISE 6: Reverse Array ===\n\n");
    
    printf("Original: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    
    int *start = arr;
    int *end = arr + size - 1;
    
    while (start < end) {
        // Swap using pointers
        int temp = *start;
        *start = *end;
        *end = temp;
        
        start++;
        end--;
    }
    
    printf("Reversed: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}


/* ----- SOLUTION 7: Find Maximum ----- */
void solution7_find_maximum(void) {
    int arr[7] = {23, 45, 67, 12, 90, 34, 56};
    int size = 7;
    
    printf("=== EXERCISE 7: Find Maximum ===\n\n");
    
    int *ptr = arr;
    int *max_ptr = arr;
    int *end = arr + size;
    
    while (ptr < end) {
        if (*ptr > *max_ptr) {
            max_ptr = ptr;
        }
        ptr++;
    }
    
    printf("Maximum value: %d\n", *max_ptr);
    printf("Position: %ld\n", max_ptr - arr);
}


/* ----- SOLUTION 8: Copy Array ----- */
void solution8_copy_array(void) {
    int source[5] = {10, 20, 30, 40, 50};
    int dest[5];
    int size = 5;
    
    printf("=== EXERCISE 8: Copy Array ===\n\n");
    
    int *src_ptr = source;
    int *dst_ptr = dest;
    int *end = source + size;
    
    while (src_ptr < end) {
        *dst_ptr = *src_ptr;
        src_ptr++;
        dst_ptr++;
    }
    
    printf("Source: ");
    for (int i = 0; i < size; i++) printf("%d ", source[i]);
    printf("\n");
    
    printf("Destination: ");
    for (int i = 0; i < size; i++) printf("%d ", dest[i]);
    printf("\n");
}


/* ----- SOLUTION 9: Bubble Sort ----- */
void solution9_bubble_sort(void) {
    int arr[7] = {64, 34, 25, 12, 22, 11, 90};
    int size = 7;
    
    printf("=== EXERCISE 9: Bubble Sort with Pointers ===\n\n");
    
    printf("Before: ");
    for (int i = 0; i < size; i++) printf("%d ", arr[i]);
    printf("\n");
    
    for (int i = 0; i < size - 1; i++) {
        int *ptr = arr;
        int *end = arr + size - 1 - i;
        
        while (ptr < end) {
            if (*ptr > *(ptr + 1)) {
                int temp = *ptr;
                *ptr = *(ptr + 1);
                *(ptr + 1) = temp;
            }
            ptr++;
        }
    }
    
    printf("After:  ");
    for (int i = 0; i < size; i++) printf("%d ", arr[i]);
    printf("\n");
}


/* ----- SOLUTION 10: Pointer Difference ----- */
void solution10_pointer_difference(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    
    printf("=== EXERCISE 10: Pointer Difference ===\n\n");
    
    int *ptr1 = &arr[1];
    int *ptr2 = &arr[4];
    
    printf("ptr1 points to index 1 (value: %d)\n", *ptr1);
    printf("ptr2 points to index 4 (value: %d)\n", *ptr2);
    printf("Element difference: %ld\n", ptr2 - ptr1);
    printf("Byte difference: %ld\n", (long)((char*)ptr2 - (char*)ptr1));
}


/* ----- SOLUTION 11: 2D Array with Pointers ----- */
void solution11_2d_array_pointers(void) {
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    printf("=== EXERCISE 11: 2D Array with Pointers ===\n\n");
    
    // Access matrix[1][2] using pointer notation
    printf("matrix[1][2] = %d\n", *(*(matrix + 1) + 2));
    
    printf("Full matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", *(*(matrix + i) + j));
        }
        printf("\n");
    }
}


/* ----- SOLUTION 12: Array Comparison ----- */
void solution12_array_comparison(void) {
    int arr1[5] = {10, 20, 30, 40, 50};
    int arr2[5] = {10, 20, 30, 40, 50};
    int arr3[5] = {10, 20, 30, 99, 50};
    int size = 5;
    
    printf("=== EXERCISE 12: Array Comparison ===\n\n");
    
    // Compare arr1 and arr2
    int *p1 = arr1;
    int *p2 = arr2;
    int equal = 1;
    int diff_idx = -1;
    
    for (int i = 0; i < size; i++) {
        if (*(p1 + i) != *(p2 + i)) {
            equal = 0;
            diff_idx = i;
            break;
        }
    }
    
    printf("Comparing arr1 and arr2: %s\n", equal ? "EQUAL" : "NOT EQUAL");
    
    // Compare arr1 and arr3
    p2 = arr3;
    equal = 1;
    diff_idx = -1;
    
    for (int i = 0; i < size; i++) {
        if (*(p1 + i) != *(p2 + i)) {
            equal = 0;
            diff_idx = i;
            break;
        }
    }
    
    if (equal) {
        printf("Comparing arr1 and arr3: EQUAL\n");
    } else {
        printf("Comparing arr1 and arr3: NOT EQUAL (differs at index %d)\n", diff_idx);
    }
}


/* ----- SOLUTION 13: Merge Sorted Arrays ----- */
void solution13_merge_arrays(void) {
    int arr1[4] = {1, 3, 5, 7};
    int arr2[4] = {2, 4, 6, 8};
    int merged[8];
    int size1 = 4, size2 = 4;
    
    printf("=== EXERCISE 13: Merge Sorted Arrays ===\n\n");
    
    printf("Array 1: ");
    for (int i = 0; i < size1; i++) printf("%d ", arr1[i]);
    printf("\n");
    
    printf("Array 2: ");
    for (int i = 0; i < size2; i++) printf("%d ", arr2[i]);
    printf("\n");
    
    int *p1 = arr1;
    int *p2 = arr2;
    int *pm = merged;
    int *end1 = arr1 + size1;
    int *end2 = arr2 + size2;
    
    while (p1 < end1 && p2 < end2) {
        if (*p1 <= *p2) {
            *pm++ = *p1++;
        } else {
            *pm++ = *p2++;
        }
    }
    
    while (p1 < end1) {
        *pm++ = *p1++;
    }
    
    while (p2 < end2) {
        *pm++ = *p2++;
    }
    
    printf("Merged:  ");
    for (int i = 0; i < size1 + size2; i++) {
        printf("%d ", merged[i]);
    }
    printf("\n");
}


/* ----- SOLUTION 14: Search with Pointer Return ----- */
int* solution_linear_search(int *arr, int size, int target) {
    int *ptr = arr;
    int *end = arr + size;
    
    while (ptr < end) {
        if (*ptr == target) {
            return ptr;
        }
        ptr++;
    }
    return NULL;
}

void solution14_search_with_pointer(void) {
    int arr[5] = {10, 20, 30, 40, 50};
    int size = 5;
    
    printf("=== EXERCISE 14: Search with Pointer Return ===\n\n");
    
    // Search for 30
    int *found = solution_linear_search(arr, size, 30);
    if (found != NULL) {
        printf("Searching for 30: Found at position %ld\n", found - arr);
        printf("Modifying 30 to 300\n");
        *found = 300;
        
        printf("Array after modification: ");
        for (int i = 0; i < size; i++) {
            printf("%d ", arr[i]);
        }
        printf("\n");
    }
    
    // Search for 99
    found = solution_linear_search(arr, size, 99);
    if (found == NULL) {
        printf("Searching for 99: Not found\n");
    }
}


/* ----- SOLUTION 15: String Operations ----- */
int solution_strlen(const char *str) {
    const char *ptr = str;
    while (*ptr != '\0') {
        ptr++;
    }
    return ptr - str;
}

void solution_strcpy(char *dest, const char *src) {
    while (*src != '\0') {
        *dest = *src;
        dest++;
        src++;
    }
    *dest = '\0';
}

void solution15_string_operations(void) {
    const char *original = "Hello, World!";
    char copy[50];
    
    printf("=== EXERCISE 15: String Operations ===\n\n");
    
    printf("Original: \"%s\"\n", original);
    
    int len = solution_strlen(original);
    printf("Length: %d\n", len);
    
    solution_strcpy(copy, original);
    printf("Copy: \"%s\"\n", copy);
}
Exercises - C Programming Tutorial | DeepML