Answer to Question #285067 in C for sunny

Question #285067

What's in There?

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:

  1. Create an empty character array with a size of 100.
  2. Use an appropriate input function for strings and store the value into the empty array.
  3. Input a random integer. It shall only be any integer that's less than the string's length.
  4. Using your understanding on accessing list elements, access and print out the array element having the index position of the inputted random integer

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
1
Expert's answer
2022-01-06T02:01:37-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS