This blog is under construction

Saturday 29 June 2013

C program to access string using pointers

Write a C program to access string using pointers.


  #include <stdio.h>
  #include <string.h>
  int main() {
        char str[100], *ptr;
        int i;

        /* get the input string from the user */
        printf("Enter your input:\n");
        fgets(str, 100, stdin);
        str[strlen(str) - 1] = '\0';

        /* ptr points to str address */
        ptr = str;

        /* accessing string using pointer */
        while (*ptr != '\0') {
                printf("%c", *ptr);
                ptr++;
        }
        printf("\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:
  see-programming.blogspot.com
  see-programming.blogspot.com



No comments:

Post a Comment