C program to Print String using Pointer

Write a C program to print string using pointer. In general, we use a loop to iterate each character index of the string array. In this C example, we assigned the string array address to the pointer. Next, we used a while loop to print each character by incrementing the pointer.

#include <stdio.h>

int main()
{
    char str[100];
    char *ch;

    printf("Please Enter String to print = ");
    fgets(str, sizeof str, stdin);

    ch = str;
    printf("\nPrinting Given String using Pointers\n");
    while(*ch != '\0')
    {
        printf("%c", *ch++);
    }
}
C program to Print String using Pointer

This c program uses the for loop and prints the string or character array using a pointer. In this c example, we have shown another option by creating a printString function that accepts a pointer and prints the string.

#include <stdio.h>
void printString(char *ch)
{
    while(*ch != '\0')
    {
        printf("%c", *ch++);
    }
}
int main()
{
    char str[100];
    char *ch;

    printf("Please Enter String to print = ");
    fgets(str, sizeof str, stdin);

    printf("\nPrinting Given String using Pointers\n");
    for(ch = str; *ch != '\0'; ch++)
    {
        printf("%c", *ch);
    }
    printString(str);
}
Please Enter String to print = c programs output

Printing Given String using Pointers
c programs output
c programs output