w3resource

C Exercises: Print all the alphabets

C Pointer : Exercise-21 with Solution

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

Visual Presentation:

C Exercises: Pictorial: Print all the alphabets.

Sample Solution:

C Code:

#include <stdio.h>

int main() {
    // Declaration of variables
    char alph[27];  // Array to store alphabets
    int x;
    char *ptr;  // Pointer to char

    // Displaying the purpose of the program
    printf("\n\n Pointer : Print all the alphabets:\n");
    printf("----------------------------------------\n");

    ptr = alph; // Assigning the base address of array 'alph' to pointer 'ptr'

    // Storing alphabets in the array using pointer arithmetic
    for (x = 0; x < 26; x++) {
        *ptr = x + 'A'; // Assigning ASCII values of alphabets to the array through the pointer
        ptr++; // Moving the pointer to the next memory location
    }

    ptr = alph; // Resetting the pointer to the base address of array 'alph'

    // Printing the stored alphabets using the pointer
    printf(" The Alphabets are : \n");
    for (x = 0; x < 26; x++) {
        printf(" %c ", *ptr); // Printing each alphabet
        ptr++; // Moving the pointer to the next memory location
    }
    printf("\n\n");
    return 0;
}

Sample Output:

 Pointer : Print all the alphabates:                                                                          
----------------------------------------                                                                      
 The Alphabates are :                                                                                         
 A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z 

Flowchart:

Flowchart: Print all the alphabates

C Programming Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a program in C to show a pointer to an array which contents are pointer to structure.
Next: Write a program in C to print a string in reverse using a pointer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.