Question

I need help implementing the following code without using filehandling and only getchar(); this is the...

I need help implementing the following code without using filehandling and only getchar(); this is the prompt

"The input for your program will be a text file containing a large amount of English. Typically, an English sentence ends with a period (aka, dot). Many years ago, when people used mechanical typewriters, the proper form was to place one space between words in a sentence, but two spaces after the period at the end of the sentence. This rule is no longer taught, since word processors automatically control spacing. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is the ASCII encoding of a single character in the “secret message”. Your program will scan the input, and for each period (aka, dot) encountered, your program will count the number of spaces (ASCII 0x20) that immediately follow the dot. If the number of spaces is 0, then your program will ignore that dot completely. If the number of spaces is 1, then that corresponds to a single 0 bit of the secret message. If the number of spaces is 2, then that corresponds to a single 1 bit of the secret message. Finally, if the number of spaces is 3 or more, then that indicates that there are no more characters in the secret message. Your program must scan the input file, and output to stdout the plain text secret message. If your program successfully completes its task, then the program should return an exit status of 0 to the operating system. If the number of bits in the message is not a multiple of 8 (8 bits for each ASCII character), then your program should return an exit status of 1. If the input file does not containing the “message over” signal, i.e., a dot followed by 3 or more spaces, then your program should return an exit status of 2."

and this is my code

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main(){
  
  
  
char ch;
int count = 0;
int secretMessage = 0;
int bit = 0;
int flag = 0;
  
while ((ch = getchar()) != EOF){

while ((ch = getchar()) == '.'){

while ((ch = getchar()) == ' '){

if(ch==' ') {   
count++;
}
else
{ break;}
}
  


if(count == 2){   
int mask=1<<bit;
secretMessage=secretMessage | mask;
bit++;
}
else if(count == 1)   
{
int mask=1<<bit;
secretMessage=secretMessage & mask;
bit++;

bit++;}
else if(count >= 3){   
flag = 1;
break;


if(bit == 8){
bit = 0;
printf("%c", secretMessage);
secretMessage = 0;
}
}

}

}

if(bit != 0){
return(1);
}
if(flag == 0){
return(2);
}
  
return 0;
}
  
i am not sure where i went wrong

  

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

Answer:-


#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main(){
  
   FILE *file;
   if( (file = fopen("cipher.txt", "r")) == NULL){       //open file
       printf("Error in opening file");
       exit(1);
   }
  
   char ch;
   int count = 0;
   int ascii = 0;
   int bit = 0;
   int flag = 0;
  
   while ((ch = fgetc(file)) != EOF){       //read a character
       if(ch == '.'){                   //if found a dot
           count = 0;
           while ((ch = fgetc(file)) != EOF){
               if(ch==' ')           //count no of spaces
                   count++;
               else
                   break;
           }
           if(count == 2){           //if 2 spaces present -- 1 bit
               ascii += pow(2,bit);
               bit++;
           }
           else if(count == 1)       //if 1 space present -- 0 bit
               bit++;
           else if(count >= 3){       //if more than 3 spaces present -- no more message
               flag = 1;
               break;
           }
       }
       if(bit == 8){
           bit = 0;
           printf("%c", ascii);       //print character of message when 8 bits are read
           ascii = 0;
       }
   }
  
   fclose(file);
  
   if(bit != 0){
       exit(1);
   }
   if(flag == 0){
       exit(2);
   }
  
   return 0;
}

If you find difficulty to understand the code, please let me know in comments tab below, Then I will provide another code or else i will any modifications in the present code. Hope it will helps you. Please give Thumbs Up!! Thank you for posting the question, All the best.

Add a comment
Know the answer?
Add Answer to:
I need help implementing the following code without using filehandling and only getchar(); this is the...
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
  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

  • Write a program that reads a sentence input from the user. The program should count the...

    Write a program that reads a sentence input from the user. The program should count the number of vowels(a,e,i,o,u) in a sentence. Use the following function to read the sentence. Should use switch statement with toupper. int read_line(char str[],int n) {    int ch, i=0;    while((ch =getchar()) != '\n')        if(1<n)            str[i++]==ch;    str[i] != '\0';    return i; } output should look like: Enter a sentence: and thats the way it is! Your sentence...

  • I am trying to figure out why my C code is outputting "exited, segmentation fault". The...

    I am trying to figure out why my C code is outputting "exited, segmentation fault". The game is supposed to generate 4 random numbers and store them in the "secret" array. Then the user is suppose to guess the secret code. The program also calculates the score of the user's guess. For now, I printed out the random secret code that has been generated, but when the game continues, it will output "exited, segmentation fault". Also, the GetSecretCode function has...

  • C Language! Please, comment what each line of code does in the code below and answer...

    C Language! Please, comment what each line of code does in the code below and answer this question: (sentence is fine) 1. Aside from pressing ctrl-(d/z) at the beginning of a line causing the getnchar() function to return NULL, is there any way to enter less than 9 characters? Code: void exer1(void) {    char input[LEN];    char *check; getchar(); // Clearing character buffer. printf("Please enter 9 characters: "); // Prompting the user to enter // a specific input.   ...

  • I need help finding what is wrong with this code, it is for a CS course...

    I need help finding what is wrong with this code, it is for a CS course I am taking which codes in C (I am using XCode on Mac to code). This is the assignment: "Write a program that performs character processing on 10 characters read in from a file, and writes the results to output files. The program should read from “input.dat”. The program should write the ASCII values of the characters to “output_ascii.dat”. The program should print statistics...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • For this code after case 0, I need it to ask the user if they would...

    For this code after case 0, I need it to ask the user if they would like to see the menu again and some type of if statement or loop to ask them yes or no and if yes print again and if no end program. So it would ask if they would like to see the menu again then ask what their choice is. If yes print menu and if no end program. import java.util.InputMismatchException; import java.util.*; import java.io.*;...

  • I need help understanding this programming assignment. I do not understand it at all. Someone provided...

    I need help understanding this programming assignment. I do not understand it at all. Someone provided me with the code but when I run the code on eclipse it gives an error. Please explain this assignment to me please. Here is the code: import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class TextEditor { public static List<String> lines = new ArrayList<String>(); public static void main(String[] args) throws IOException { Scanner s = new...

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