C program to Print String using Pointer

Write a C program to print string using a pointer. In general, we use a loop to iterate each character index of the string array. In this 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 character array using a pointer. In this 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 Text = ");
    fgets(str, sizeof str, stdin);

    for(ch = str; *ch != '\0'; ch++)
    {
        printf("%c", *ch);
    }
    printString(str);
}
Please Enter Text = programs output

programs output
programs output