C program to print a string character by character using pointer

By IncludeHelp Last updated : March 10, 2024

In this C program, we are going to learn how to read and print (character by character) a string using pointer?

Printing a string character by character using pointer

Here, we have two variables, str is a string variable and ptr is a character pointer, that will point to the string variable str.

First of all, we are reading string in str and then assigning the base address of str to the character pointer ptr by using ptr=str or it can also be done by using ptr = &str[0].

And, finally we are printing the string character by character until NULL not found. Characters are printing by the pointer *ptr.

Consider the program

C program to print a string character by character using pointer

/*C program to print a string using pointer.*/
#include <stdio.h>
int main()
{
    char str[100];
    char *ptr;
     
    printf("Enter a string: ");
    gets(str);
     
    //assign address of str to ptr
    ptr=str;
     
    printf("Entered string is: ");
    while(*ptr!='\0')
        printf("%c",*ptr++);
         
    return 0;
}

Output

Enter a string: This is a test string.
Entered string is: This is a test string.

C Pointer Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.