Question

Write a C program that reads characters from a text file, and recognizes the identifiers in...

  1. Write a C program that reads characters from a text file, and recognizes the identifiers in the text file. It ignores the rest of the characters. An identifier is a sequence of letters, digits, and underscore characters, where the first character is always a letter or an underscore character. Lower and upper case characters can be part of the identifier. The recognized identifier are copied into the output text.

b. Change the C program in exercise 2, so that all identifiers include only upper case letters. So, if the input file includes the identifier _abCd1_9t the output file will include the corresponding identifier _ABCD1_9T

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

Here is the C code for the first problem (i.e. Copying all identifiers to output.txt file) :

#include <stdbool.h> #include <stdio.h> #include <string.h> #include <stdlib.h> // Returns 'true' if the character is a DELIMITER. bool isDelimiter(char ch) {  if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||                 ch == '/' || ch == ',' || ch == ';' || ch == '>' ||          ch == '<' || ch == '=' || ch == '(' || ch == ')' ||          ch == '[' || ch == ']' || ch == '{' || ch == '}')               return (true);  return (false); } // Returns 'true' if the string is a VALID IDENTIFIER. bool validIdentifier(char* str) {      if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||          str[0] == '3' || str[0] == '4' || str[0] == '5' ||              str[0] == '6' || str[0] == '7' || str[0] == '8' ||              str[0] == '9' || isDelimiter(str[0]) == true)           return (false);         return (true); } // Extracts the SUBSTRING. char* subString(char* str, int left, int right) {   int i;  char* subStr = (char*)malloc(                           sizeof(char) * (right - left + 2));     for (i = left; i <= right; i++)              subStr[i - left] = str[i];      subStr[right - left + 1] = '\0';        return (subStr); } // Parsing the input STRING. void parse(char* str) {         int left = 0, right = 0;        int len = strlen(str);  while (right <= len && left <= right) {           if (isDelimiter(str[right]) == false)                   right++;                if (isDelimiter(str[right]) == true && left == right) { right++;                        left = right;           } else if (isDelimiter(str[right]) == true && left != right || (right == len && left != right)) {                       char* subStr = subString(str, left, right - 1); if (validIdentifier(subStr) == true && isDelimiter(str[right - 1]) == false)                    { FILE *fptr; fptr = fopen("output.txt", "w"); fgets(sentence, sizeof(sentence), stdin); fprintf(fptr, "%s", str); fclose(fptr); } left = right;                }       }       return; } // DRIVER FUNCTION int main() {       // maximum legth of string is 100 here  char str[100] = "int a = b + 1c; "; char filename[20]; printf("Enter the file name : ",filename); scanf("%s",filename); FILE * fp = fopen(filename, "r"); if (fp == NULL) return 1;     char c; char buf[10];   while(fscanf(fp, "%s", buf) != EOF)     {               if(c == ' ' || c == '\n')                continue;              else parse(buf);        }       fclose(fp);     return (0); }

Here is the C code for the second problem (i.e. Copying all identifiers to output.txt file in the uppercased form) :

#include <stdbool.h> #include <stdio.h> #include <string.h> #include <stdlib.h> // Returns 'true' if the character is a DELIMITER. bool isDelimiter(char ch) {      if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||                 ch == '/' || ch == ',' || ch == ';' || ch == '>' ||          ch == '<' || ch == '=' || ch == '(' || ch == ')' ||          ch == '[' || ch == ']' || ch == '{' || ch == '}')               return (true);  return (false); } // Returns 'true' if the string is a VALID IDENTIFIER. bool validIdentifier(char* str) {      if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||          str[0] == '3' || str[0] == '4' || str[0] == '5' ||              str[0] == '6' || str[0] == '7' || str[0] == '8' ||              str[0] == '9' || isDelimiter(str[0]) == true)           return (false);         return (true); } // Extracts the SUBSTRING. char* subString(char* str, int left, int right) {   int i;  char* subStr = (char*)malloc(                           sizeof(char) * (right - left + 2));     for (i = left; i <= right; i++)              subStr[i - left] = str[i];      subStr[right - left + 1] = '\0';        return (subStr); } // Parsing the input STRING. void parse(char* str) {         int left = 0, right = 0;        int len = strlen(str);  while (right <= len && left <= right) {           if (isDelimiter(str[right]) == false)                   right++;                if (isDelimiter(str[right]) == true && left == right) { right++;                        left = right;           } else if (isDelimiter(str[right]) == true && left != right || (right == len && left != right)) {                       char* subStr = subString(str, left, right - 1); if (validIdentifier(subStr) == true && isDelimiter(str[right - 1]) == false)                    { for(i=0; str[i]!='\0'; i++) if(str[i]>='a' && str[i]<='z') str[i] = str[i] - 32; FILE *fptr; fptr = fopen("output.txt", "w"); fgets(sentence, sizeof(sentence), stdin); fprintf(fptr, "%s", str); fclose(fptr); } left = right;                 }       }       return; } // DRIVER FUNCTION int main() {       // maximum legth of string is 100 here  char str[100] = "int a = b + 1c; "; char filename[20]; printf("Enter the file name : ",filename); scanf("%s",filename); FILE * fp = fopen(filename, "r"); if (fp == NULL) return 1;     char c; char buf[10];   while(fscanf(fp, "%s", buf) != EOF)     {               if(c == ' ' || c == '\n')                continue;              else parse(buf);        }       fclose(fp);     return (0); } 
Add a comment
Know the answer?
Add Answer to:
Write a C program that reads characters from a text file, and recognizes the identifiers in...
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
  • write a C++ program that reads in a text file and reads each character or lexemes...

    write a C++ program that reads in a text file and reads each character or lexemes and states what token it is and writes to another file for example: while (fahr < upper) a = 23.00 whileend Output: token lexeme -------------------------------- keyword while separator ( identifier fahr operator < identifier upper separator ) identifier a operator = real 23.00 keyword whileend the program aslo reads in comments but does not add to the list. characters that are commented out or...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • 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...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • 1. Write a python program that reads a file and prints the letters in increasing order...

    1. Write a python program that reads a file and prints the letters in increasing order of frequency. Your program should convert entire input to lower case and only counts letters a-z. Special characters or spaces should not be counted. Each letter and it's occurrences should be listed on a separate line. 2. Test and verify your program and it's output. ---Please provide a code and a screenshot of the code and output. Thank you. ----

  • In Python 3, Write a program that reads in a text file that consists of some...

    In Python 3, Write a program that reads in a text file that consists of some standard English text. Your program should count the number of occurrences of each letter of the alphabet, and display each letter with its count, in the order of increasing count. What are the six most frequently used letters?

  • C++ please Write a program that reads the following sentences from a file: I am Sam...

    C++ please Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with the...

  • please use c++ please write down the input file information please add comments for some codes...

    please use c++ please write down the input file information please add comments for some codes please do not use #include <bits/stdc++.h> we did not learn it yet thank you CSIT 575-Take Home Lab #11: Arrays To learn to code, compile and run a program processing characters. Assignment Plan and code a top-down modular program utilizing ARRAYS to solve the following problem, using at least 3 functions (called from the main() section or from another function) to solve the problem....

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