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

I need the input function is like ... input( char * file.txt) and the return out

ID: 3749804 • Letter: I

Question

I need the input function is like ... input( char * file.txt) and the return output include 2 components above such as return (array_string[ ],array_char[ ]). I need the function output as the function for next step. Such as return ( Bob, w). Thank you. Writing the C code reading ", also whitespace(" ") and tab("It"). The output includes storing 2 components: First one is the name such as "bob", the second one is a letter such as "r", "w", "b". (Hint: using open) the file.txt below. Ignoring the line of comment ".# # this is a comme nt alice r bob claireb # this is another comment

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
/* Change N according to the data in file */

typedef struct L{
int length;
char *name[N];
char letters[N];
} Line;

Line returnLine(char * filename){
FILE *fp;
char * line = NULL;
size_t len = 0,read,i=0;
Line var;
fp = fopen(filename,"r");
/* Opened the file in read-mode */

/* while loop here is reading each line and storing the line in line variable */
while ((read = getline(&line, &len, fp)) != -1) {
        if (line[0] == '#') continue;
   /* discarding the lines that begins with # */
   size_t index = 0,l=1;

   /* You can change isalpha to isalnum if the name-string has numbers in it */
   /* looking for the very first character */
   while ( isalpha(line[index]) == 0 ) index++;

   while ( isalpha(line[index+l]) ) l++;

   var.name[i] = malloc(l);
   strncpy(var.name[i],line+index,l);
   /* copying the sub-string to the variable */
   index+=l;

   /* looking for the letter(char) */
   while (isalpha(line[index]) == 0) index++;
   var.letters[i] = line[index];
   i++;
    }
var.length = i;
fclose(fp);
return var;
}

int main(){
/* filename will include the full path of file if file not present in current directory
     or just the filename if present in current directory
   */
char filename[]="file.txt";
Line var = returnLine(filename);

size_t i =0;
while (i < var.length) {
      printf("%s , %c ",var.name[i],var.letters[i]);
      i++;
}

return 0;
}

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