Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Print message based on character indices of string (in C) I have a text file tha

ID: 3682075 • Letter: P

Question

Print message based on character indices of string (in C)

I have a text file that looks like this:

-The numbers could be grouped in 2's so something like

(0,0),(1,1),(2,0) (3,0)(4,1)(4,2)

-The first num in the pair is the index of a word and the second num is index of a character in the word.

Then, I have a 2d char array like this:

char arr[5][4];

arr[0]=hat;

arr[1]=met;

arr[2]=yes;

arr[3]=yawn;

arr[4]=fou;

arr[5]=tom;

So here, the printed message would be "hey you". The space in the text file between pairs of numbers should be shown in the printed message.

Please show how to read in the indices from file and use it to arrive at the final printed message.

Explanation / Answer

Here is the code for you:

#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("MessageFromIndices.txt", "r");
char arr[6][4] = {"hat", "met", "yes", "yawn", "fou", "tom"};
char ch;
int first, second;
int flag = 0;
while(!feof(fp))
{
fscanf(fp, "%c", &ch);
printf("%c", ch);
if(ch == ' ')
printf(" ");
else
{
if(flag == 0 && ch != ',')
{
first = ch - '0';
flag = 1;
continue;
}
else if(flag == 1 && ch != ',')
{
second = ch - '0';
flag = 0;
printf("%c", arr[first][second]);
}
}
}
printf(" ");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote