by CodeChum Admin
Let's try defining the size of the array and create the contents of it on our own! And then, to give off a sense of excitement, let's try accessing the value of an array element in a random index position!
Let's do this fast!
Instructions:
Input
1. A string
2. An index
Output
The first line will contain a message prompt to input the string.
The second line will contain a message prompt to input the integer which represents the index.
The last line contains the character at the index.
Enter·the·string:·Cody
Enter·the·index:·3
Character·at·index·3·=·y
#include <stdio.h>
int main()
{
//Create an empty character array with a size of 100.
char string[100];
int index;
printf("Enter the string: ");
gets(string);
//Use an appropriate input function for strings and store the value into the empty array.
//Input a random integer. It shall only be any integer that's less than the string's length.
printf("Enter the index: ");
scanf("%d",&index);
//Using your understanding on accessing list elements, access and print out the array element
//having the index position of the inputted random integer
printf("Character at index %d = %c",index,string[index]);
scanf("%d", &index);
return 0;
}
Comments
Leave a comment