C Programing-Questions On Pointers : Exercises, Practice, Solution

Write a C program to swap two integers using pointers.

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 5, y = 10;
    
    printf("Before swapping: x = %d, y = %d\n", x, y);
    
    swap(&x, &y);
    
    printf("After swapping: x = %d, y = %d\n", x, y);
    
    return 0;
}

Write a C program to find the sum of elements in an array using pointers.

#include <stdio.h>

int arraySum(int *arr, int size) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += *(arr + i);
    }
    return sum;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    printf("Sum of array elements: %d\n", arraySum(arr, size));
    
    return 0;
}

Write a C program to reverse a string using pointers.

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

void reverseString(char *str) {
    int length = strlen(str);
    char *start = str;
    char *end = str + length - 1;
    
    while (start < end) {
        char temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}

int main() {
    char str[] = "hello";
    
    printf("Before reversing: %s\n", str);
    
    reverseString(str);
    
    printf("After reversing: %s\n", str);
    
    return 0;
}

Write a C program to find the maximum element in an array using pointers.

#include <stdio.h>

int findMax(int *arr, int size) {
    int max = *arr;
    for (int i = 1; i < size; i++) {
        if (*(arr + i) > max) {
            max = *(arr + i);
        }
    }
    return max;
}

int main() {
    int arr[] = {5, 10, 3, 8, 15};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    printf("Maximum element in array: %d\n", findMax(arr, size));
    
    return 0;
}

Write a C program to count the number of vowels in a string using pointers.

#include <stdio.h>
#include <ctype.h>

int countVowels(char *str) {
    int count = 0;
    while (*str != '\0') {
        char c = tolower(*str);
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            count++;
        }
        str++;
    }
    return count;
}

int main() {
    char str[] = "Hello, World!";
    
    printf("Number of vowels: %d\n", countVowels(str));
    
    return 0;
}

Write a C program to concatenate two strings using pointers.

#include <stdio.h>

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

int main() {
    char str1[20] = "Hello";
    char str2[] = " World!";
    
    concatenateStrings(str1, str2);
    
    printf("Concatenated string: %s\n", str1);
    
    return 0;
}

Write a C program to count the occurrences of a character in a string using pointers.

#include <stdio.h>

int countOccurrences(char *str, char ch) {
    int count = 0;
    while (*str != '\0') {
        if (*str == ch) {
            count++;
        }
        str++;
    }
    return count;
}

int main() {
    char str[] = "hello world";
    char ch = 'l';
    
    printf("Occurrences of '%c': %d\n", ch, countOccurrences(str, ch));
    
    return 0;
}

Write a C program to check if a string is a palindrome using pointers.

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

bool isPalindrome(char *str) {
    int length = strlen(str);
    char *start = str;
    char *end = str + length - 1;
    
    while (start < end) {
        if (*start != *end) {
            return false;
        }
        start++;
        end--;
    }
    return true;
}

int main() {
    char str[] = "radar";
    
    if (isPalindrome(str)) {
        printf("The string is a palindrome.\n");
    } else {
        printf("The string is not a palindrome.\n");
    }
    
    return 0;
}

Write a C program to find the length of a string using pointers.

#include <stdio.h>

int stringLength(char *str) {
    int length = 0;
    while (*str != '\0') {
        length++;
        str++;
    }
    return length;
}

int main() {
    char str[] = "hello";
    
    printf("Length of string: %d\n", stringLength(str));
    
    return 0;
}

Write a C program to copy one string to another using pointers.

#include <stdio.h>

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

int main() {
    char str1[20];
    char str2[] = "Hello";
    
    stringCopy(str1, str2);
    
    printf("Copied string: %s\n", str1);
    
    return 0;
}

Write a C program to find the factorial of a number using pointers.

#include <stdio.h>

int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}

int main() {
    int num = 5;
    
    printf("Factorial of %d: %d\n", num, factorial(num));
    
    return 0;
}

Write a C program to sort an array of integers using pointers.

#include <stdio.h>

void bubbleSort(int *arr, int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (*(arr + j) > *(arr + j + 1)) {
                int temp = *(arr + j);
                *(arr + j) = *(arr + j + 1);
                *(arr + j + 1) = temp;
            }
        }
    }
}

int main() {
    int arr[] = {5, 2, 8, 1, 4};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    bubbleSort(arr, size);
    
    printf("Sorted array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    
    return 0;
}

Write a C program to reverse an array using pointers.

#include <stdio.h>

void reverseArray(int *arr, int size) {
    int *start = arr;
    int *end = arr + size - 1;
    
    while (start < end) {
        int temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    reverseArray(arr, size);
    
    printf("Reversed array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    
    return 0;
}

Write a C program to check if two strings are anagrams using pointers.

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

bool areAnagrams(char *str1, char *str2) {
    int count[256] = {0};
    
    while (*str1 != '\0') {
        count[(int)*str1]++;
        str1++;
    }
    while (*str2 != '\0') {
        count[(int)*str2]--;
        str2++;
    }
    for (int i = 0; i < 256; i++) {
        if (count[i] != 0) {
            return false;
        }
    }
    return true;
}

int main() {
    char str1[] = "listen";
    char str2[] = "silent";
    
    if (areAnagrams(str1, str2)) {
        printf("The strings are anagrams.\n");
    } else {
        printf("The strings are not anagrams.\n");
    }
    
    return 0;
}

Write a C program to find the largest and smallest elements in an array using pointers.

#include <stdio.h>

void findMinMax(int *arr, int size, int *max, int *min) {
    *max = *min = *arr;
    for (int i = 1; i < size; i++) {
        if (*(arr + i) > *max) {
            *max = *(arr + i);
        }
        if (*(arr + i) < *min) {
            *min = *(arr + i);
        }
    }
}

int main() {
    int arr[] = {5, 10, 3, 8, 15};
    int size = sizeof(arr) / sizeof(arr[0]);
    int max, min;
    
    findMinMax(arr, size, &max, &min);
    
    printf("Largest element: %d\n", max);
    printf("Smallest element: %d\n", min);
    
    return 0;
}

Write a C program to multiply two matrices using pointers.

#include <stdio.h>

void matrixMultiply(int *mat1, int *mat2, int *result, int rows1, int cols1, int cols2) {
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < cols2; j++) {
            *(result + i * cols2 + j) = 0;
            for (int k = 0; k < cols1; k++) {
                *(result + i * cols2 + j) += *((mat1 + i * cols1) + k) * *((mat2 + k * cols2) + j);
            }
        }
    }
}

void displayMatrix(int *matrix, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", *((matrix + i * cols) + j));
        }
        printf("\n");
    }
}

int main() {
    int mat1[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };
    int mat2[3][2] = {
        {7, 8},
        {9, 10},
        {11, 12}
    };
    int result[2][2];
    
    matrixMultiply((int *)mat1, (int *)mat2, (int *)result, 2, 3, 2);
    
    printf("Resultant matrix:\n");
    displayMatrix((int *)result, 2, 2);
    
    return 0;
}


Write a program in C to print all the alphabets using a pointer.

#include <stdio.h>

int main() {
    char *ptr; // Declare a pointer to char
    
    printf("Alphabets: ");
    
    // Initialize the pointer to point to the starting address of the alphabet 'A'
    ptr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    // Loop through the string until the null terminator is reached
    while (*ptr != '\0') {
        printf("%c ", *ptr); // Print the character at the current pointer location
        ptr++; // Move the pointer to the next character
    }
    
    printf("\n");
    
    return 0;
}

Write a C program to print all the digits from 0 to 9 using a pointer.

#include <stdio.h>

int main() {
    char *ptr; // Declare a pointer to char
    
    printf("Digits: ");
    
    // Initialize the pointer to point to the starting address of the digits '0' to '9'
    ptr = "0123456789";
    
    // Loop through the string until the null terminator is reached
    while (*ptr != '\0') {
        printf("%c ", *ptr); // Print the character at the current pointer location
        ptr++; // Move the pointer to the next character
    }
    
    printf("\n");
    
    return 0;
}

Write a C program to print all the lowercase letters from ‘a’ to ‘z’ using a pointer.

#include <stdio.h>

int main() {
    char *ptr; // Declare a pointer to char
    
    printf("Lowercase letters: ");
    
    // Initialize the pointer to point to the starting address of the lowercase letters 'a' to 'z'
    ptr = "abcdefghijklmnopqrstuvwxyz";
    
    // Loop through the string until the null terminator is reached
    while (*ptr != '\0') {
        printf("%c ", *ptr); // Print the character at the current pointer location
        ptr++; // Move the pointer to the next character
    }
    
    printf("\n");
    
    return 0;
}

On Key

Related Posts