C Program to Input a String & Store their Ascii Values in an Integer Array & Print the Array

«
»

This is a C Program to input a string & store their Ascii Values in an Array & print the Array

Problem Description

This program will take an input of string & store their ASCII values and print them.

Problem Solution

1. Create an array of characters and take its size from users as an input.
2. Enter a string.
3. Using a loop, scan each element(character) of the string and print its equivalent ASCII value, increment the value of iterator used to access the position of characters of the string.

advertisement
Program/Source Code

Here is source code of the C Program to input a string & store their ascii values and print them. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C Program to Input a String & Store their Ascii Values in an Integer Array & Print the Array
  3.      */
  4.  
  5.     #include <stdio.h>
  6.     void main()
  7.     {
  8.  
  9.         char string[20];
  10.         int n, count = 0;
  11.  
  12.         printf("Enter the no of characters present in an array \n ");
  13.         scanf("%d", &n);
  14.  
  15.         printf(" Enter the string of %d characters \n" , n);
  16.         scanf("%s", string);
  17.  
  18.         while (count < n)
  19.         {
  20.             printf(" %c = %d\n", string[count], string[count] );
  21.             ++ count ;
  22.         }
  23.  
  24.     }
Program Explanation

1. Declare an array of characters of some fixed size, 20.
2. Take size from users as an input.
3. Now, enter a string again as an input, store it in character array declared above.
4. Run a for loop, which will scan each array element (i.e character), printing its equivalent ASCII code using %d notation and the character itself using %c inside printf() function.
5. Increment iterator, count after printf() statement to access next position.
6. Exit

advertisement
advertisement
Runtime Test Cases
Enter the no of characters present in an array
10
Enter the string of 10 characters
sanfoundry
 s = 115
 a = 97
 n = 110
 f = 102
 o = 111
 u = 117
 n = 110
 d = 100
 r = 114
 y = 121

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

advertisement

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
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 | Youtube | Instagram | Facebook | Twitter