Q:

C program to print a string character by character using pointer

belongs to collection: C pointers example programs

0

C program to print a string character by character using pointer

In this C program, we are going to learn how to read and print (character by character) a string 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program

/*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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to change the value of constant integer ... >>
<< C program to count vowels and consonants in a stri...