Home » C programs » C pointer programs

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.

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

Other C pointer programs

  1. Program to create, initialize, assign and access a pointer variable.
  2. Program to swap two numbers using pointers.
  3. Program to change the value of constant integer using pointers.
  4. Program to print a string using pointer.
  5. Program to count vowels and consonants in a string using pointer.
  6. Program to read array elements and print with addresses.
  7. Program to read and print student details using structure pointer, demonstrate example of structure with pointer.
  8. Program to print size of different types of pointer variables.
  9. Program to demonstrate example of double pointer (pointer to pointer).
  10. Program to demonstrate example of array of pointers.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.