C Program to Display ASCII value of all Characters used in the String

«
»
This C Program displays the ASCII value of all the characters used in the string. A palindrome is a word that reads the same backward as forward, e.g., madam.

Here is a source code of the C program to display ASCII value of all characters used in the string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Display ASCII value of all Characters 
  3.  * used in the String 
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. struct detail
  10. {
  11.     char c;
  12.     int ascii;
  13. };
  14.  
  15. int update(struct detail [], const char [], int);
  16.  
  17. int main()
  18. {
  19.     struct detail s[26];
  20.     char string[100], c;
  21.     int i = 0, index;
  22.  
  23.     for (i = 0; i < 26; i++)
  24.     {
  25.        s[i].c = i + 'a';
  26.        s[i].ascii = 0;
  27.     }
  28.     printf("Enter string: ");
  29.     i = 0;
  30.     do
  31.     {
  32.         fflush(stdin);
  33.         c = getchar();
  34.         string[i++] = c;
  35.         if (c == '\n')
  36.         {
  37.             break;
  38.         }
  39.         index = c - 'a';
  40.         s[index].ascii = c;
  41.     } while (1);
  42.     string[i - 1] = '\0';
  43.     printf("The string entered is: %s\n", string);
  44.  
  45.     printf("*************************\nCharacter\tASCII\n*************************\n");
  46.     for (i = 0; i < 26; i++)
  47.     {
  48.         if (s[i].ascii)
  49.         {
  50.             printf("     %c\t\t   %d\n", s[i].c, s[i].ascii);
  51.         }
  52.     }
  53.  
  54.     return 0;
  55. }

advertisement
$ gcc allcharascii.c 
$ ./a.out 
Enter string: hello world how are you    
The string entered is: hello world how are you
*************************
Character	ASCII
*************************
     a		   97
     d		   100
     e		   101
     h		   104
     l		   108
     o		   111
     r		   114
     u		   117
     w		   119
     y		   121

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn