Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Patterns

Array

2D Array

String Function Example

Pointers

Recursion Function

Structure

Excersises

Others


C Program to Access String Using Pointers


Write a C program to access string using pointer.

  • A pointer str is initialized to point to a string literal "Hello, World!".
  • The full string is printed using printf with the %s format specifier.
  • A while loop is used to traverse the string character-by-character using the pointer.
  • Each character is printed individually, and the pointer is incremented to move to the next character.
Example : pgm.c
#include <stdio.h>

int main() {
    // Declare and initialize a string using a pointer
    char *str = "Hello, World!";

    // Output the string
    printf("String: %s\n", str);

    // Print each character using the pointer
    printf("Characters in the string:\n");
    while(*str != '\0') {
        printf("%c ", *str);
        str++;
    }

    printf("\n");
    return 0;
}

Output :

String: Hello, World!
Characters in the string:
H e l l o , W o r l d !