This blog is under construction

Monday 24 June 2013

C program to print the ASCII values of all characters in a given string

Write a C program to find the ASCII values of all characters in a given string.


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

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

        /* print the ASCII values of all characters */
        printf("Character     ASCII\n");
        while (str[i] != '\0') {
                printf("   %c           %3d\n", str[i], str[i]);
                i++;
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your string:
  HelloWorld
  Character     ASCII
     H               72
     e              101
     l               108
     l               108
     o              111
    W               87
    o               111
     r              114
     l              108
    d              100



No comments:

Post a Comment