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() {
char arr[100];
int idx;
printf("Enter the string: ");
fgets(arr, 100, stdin);
printf("Enter the index: ");
scanf("%d", &idx);
if (idx < 0 || idx >= strlen(arr)) {
return 1;
}
printf("Character at intex %d = %c\n", idx, arr[idx]);
return 0;
}
Comments
Leave a comment