C PROGRAMMING ASSIGNMENT I am going to provide a Driver, which was provided and
ID: 3742767 • Letter: C
Question
C PROGRAMMING ASSIGNMENT
I am going to provide a Driver, which was provided and does not need to be revised, and MyAssignment ( which I am having trouble on and needs to be revised).
I am writing a parser that reads a file and adds words from the file into an array to be printed. Words will be printed seperately, the word count is what is returned to the main function, we are supposed to place null terminators in between words.
In the assignment, leading spaces are skipped, and words are split on these delimeters: " ; ", " " , EOF, spaces in between words. If " ; " , or " " is found directly after a word, then we are supposed to use ungetc in order to detect the character again and print it as a zero character word. Once EOF is reached we return -255 to let the driver know we are done.
If EOF is directly after a word, we are supposed to return the correct word count and then return -255
Finally, if "$" is detected to be the first character in a word, it is left out of the array and the word count is changed to negative when returned.
NOTE: I am not trying to save all of the elements into the array, each word is supposed to overwrite the previous.
Here are examples of expected output from input
Input : " trickier;too$h $master ;
" (new line and then EOF)
Expected output:
n= 8, s=[trickier]
n= 0, s=[ ] (This is here because of semicolon)
n= 5, s=[too$h]
n= - 5, s =[master]
n = 0, s=[ ] (This is here because of semicolon)
n=0, s=[ ] (This is here because of a new line)
n= - 255, s = [ ]
I have written most of the code, I just cannot figure out why my loop is infinite. Here is MyAssignment
#include "myassignment.h"
int myassignment(char *w)
{
int c;
int count = 0;
int negative = 1;
while((c= getchar()) != EOF)
{
if(count > 0 && c== ' ')
{
*w = '';
count *= negative;
return count;
}
if(c== ';')
{
*w = '';
if(count > 0)
{
ungetc(';',stdin);
}
count *= negative;
return count;
}
if(c== ' ')
{
*w = '';
if(count > 0)
{
ungetc(' ',stdin);
}
count *= negative;
return count;
}
if( c== '$' && count == 0)
{
negative= -1;
}
if(c != ' ')
{
*w++ = (char) c ;
count++;
}
}
if(count>0)
{
*w++ = '';
ungetc(c,stdin);
count *= negative;
return count;
}
*w = '';
return -255;
}
DRIVER
#include "myassignment.h"
int main()
{
int c;
char s[STORAGE];
for(;;){
(void)printf("n=%d, s=[%s] ", c = myassignment(s),s);
if(c==-255)break;
}
}
Explanation / Answer
Your question has a few errors I guess.
1. You have written word count where as your code is counting letters not words.
2. You have not provided the code inside myassignment.h without which it is imossible to know what is this program doing internally as this code works fine, you may be having trouble because of either compiler version or there are issues in your myassignment.h headerfile.Without this header file code it seems like your code getting input from user not from a file.
3. Currently there is small issue in the code you have provided and I have corrected it. Now the code works fine if he input is from a user but not a file.
Code and output it gave is on the following link according to the input you gave in your question:
https://ide.geeksforgeeks.org/37cikAPoHJ
In case of any confusion please comments.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.