Print each input character and its ASCII decimal value. - C String

C examples for String:char array

Description

Print each input character and its ASCII decimal value.

Demo Code

#include <stdio.h>

int main(void)
{
  int ch, char_count = 0;

  while ((ch = getchar()) != EOF)
  {//from   ww w  .  j  a va  2  s  . c o m
    if (ch >= ' ')
      printf("\'%c\': %d", ch, ch);
    else if (ch == '\n')
      printf("\'\\n\': %d", ch);
    else if (ch == '\t')
      printf("\'\\t\': %d", ch);
    else // ascii control characters
      printf("\'^%c\': %d", ch + 64, ch );

    char_count++;
    if (char_count % 10 == 0)
      printf("\n"); // print new line for every 10 characters
    else
      printf("  "); // otherwise, print spaces
  }

  printf("\n");

  return 0;
}

Result


Related Tutorials