Question

NO MALLOC & NO USE OF [ ] C need to parse a file and update...

NO MALLOC & NO USE OF [ ]

C need to parse a file and update array of structs while i do it.

The program reads input from stdin byte by byte (the bytes are from 0 to 255 and each byte represents an ascii symbol value) and each time the a byte occurs it the num_occurences field in the NODE struct must increase.

this is the struct

typedef struct node {
int num_occurences;   
short symbol;   
} NODE;

this is the array

NODE *node_symb[257];

please help me, again I need to traverse stdin byte by byte and increment the value of num_occurences each time the symbol is repeated. the array is empty in the beginning. I don't know how to do this please help.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

typedef struct node {
   int num_occurences;
   short symbol;
} NODE;
int main()
{
   NODE node_symb[257];
   short byte;
   int count = 0,i=0,found=-1;
   while ((byte = fgetc(stdin)) != EOF)
   {
       if (count == 0)
       {
           //node_symb[count]= (NODE*)malloc(sizeof(NODE));
           (node_symb + count)->symbol = byte;
           (node_symb+count)->symbol = byte;
           (node_symb + count)->num_occurences = 1;
           ++count;
       }
       else
       {
          
           for (i = 0; i < count; i++)
           {
               if ((node_symb+i)->symbol == byte)
               {
                   found = i;
                   break;
               }
           }
           if (found == -1)
           {
              
               (node_symb + count)->symbol = byte;
               (node_symb+count)->num_occurences = 1;
               (node_symb + count)->symbol= byte;
               ++count;
               found = -1;
           }
           else
           {
               ++(node_symb+i)->num_occurences;
               found = -1;
           }

       }
      
   }
   for (int i = 0; i < count; i++)
   {
   if((node_symb+i)->symbol == '\n')
   {
   printf("character newline found %d times \n", (node_symb+i)->num_occurences);
   }
   else if((node_symb+i)->symbol == 32)
   {
   printf("character space found %d times \n", (node_symb+i)->num_occurences);
   }
   else
       printf("character %c found %d times \n", (node_symb+i)->symbol, (node_symb+i)->num_occurences);
   }
}

=========================

//my input file

abcabda
bng da#@_
jh

//output

character a found 4 times                                                                                             

character b found 3 times                                                                                             

character c found 1 times                                                                                             

character d found 2 times                                                                                             

character newline found 2 times                                                                                       

character n found 1 times                                                                                             

character g found 1 times                                                                                             

character space found 2 times                                                                                         

character # found 1 times                                                                                             

character @ found 1 times                                                                                             

character _ found 1 times                                                                                             

character j found 1 times                                                                                             

character h found 1 times   

======================

//psl provide +ve rating , thanks

Add a comment
Know the answer?
Add Answer to:
NO MALLOC & NO USE OF [ ] C need to parse a file and update...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • Hi, I need to make a program in C that reads the type of currency and...

    Hi, I need to make a program in C that reads the type of currency and organizes them into stacks based on currency which can be read back. This is what I have so far, can I get help finishing it? #include <stdio.h> #include <stdlib.h> const float POUND = 1.31; const float YEN = 0.0091; const float RUPEE = 0.014; const float EURO = 1.11; char c; int currValue; float exchangeValue; float finValue; int printValue; struct node {    int...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • C++ need help with vector. I have a text file and input it's content into a...

    C++ need help with vector. I have a text file and input it's content into a struct that holds an array for each entry as a string type. for example: struct container{ string ID[10000]; string anotherID[10000]; } in the text file there is a huge list that looks something like this: 1 abc 3 xyz 1 abc 1 xyz How can I use a vector to sort these so I can get an output like ID: 1 and anotherID: abc...

  • I need help and tied to the requirements please. Exactly part B and D. 2. (20pts)...

    I need help and tied to the requirements please. Exactly part B and D. 2. (20pts) Consider the following linked list (LL is pointing to the sentinel node): 2. (20 points) Consider the following linked list (LL is pointing to the sentinel node) NULL Assume each node is a struct node(int data; struct node 'next) (a) what will be printed by the following statement? printf("%d %d\n",LL-> next-> next-> data, LL-> next->data); Part A: What will be printed by the following...

  • Trying to figure out what needs to be in the headers.h file. I have written the...

    Trying to figure out what needs to be in the headers.h file. I have written the print function. Qualities in header file main() needs insertionSort() and mergeSortWrapper() Both insertionSort() and mergeSortWrapper() need print(). Please copy-and-paste the following files (0 Points): insertionSort.c /*--------------------------------------------------------------------------* *---- ----* *---- insertionSort.c ----* *---- ----* *---- This file defines a function that implements insertion ----* *---- sort on a linked-list of integers. ----* *---- ----* *---- ---- ---- ---- ---- ---- ---- ---- ---- ----* *----...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • the following python code edits BMP image file to negative but I need help with modifying...

    the following python code edits BMP image file to negative but I need help with modifying this code so it turns it the same BMP image file into a black and white instead of a negative. heres python code I have so far [pasted below] ## # This program processes a digital image by creating a negative of a BMP image. # from io import SEEK_CUR from sys import exit def main() : filename = input("Please enter the file name:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT