This is a C Program to input a string & store their Ascii Values in an Array & print the Array
This program will take an input of string & store their ASCII values and print them.
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.
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.
/*
* C Program to Input a String & Store their Ascii Values in an Integer Array & Print the Array
*/
#include <stdio.h>
void main()
{
char string[20];
int n, count = 0;
printf("Enter the no of characters present in an array \n ");
scanf("%d", &n);
printf(" Enter the string of %d characters \n" , n);
scanf("%s", string);
while (count < n)
{
printf(" %c = %d\n", string[count], string[count] );
++ count ;
}
}
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
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 Books in C Programming, Data Structures and Algorithms.
- Check Computer Science Books
- Check C Books
- Apply for C Internship
- Practice BCA MCQs
- Practice Computer Science MCQs