Question

Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This file should have exactly three a-s..." charnum = char_counter('simple.txt','a') charnum = 3 You may find it helpful to download the files for testing and dubugging in MATLAB. simple.txtOpens in new tab Frankenstein-by-Shelley.txt please i need MATLAB script(code) for this assignment.

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

We will be using the inbuilt function "fileread" of Matlab that converts the text file to a character vector. After that for the length of the char vector, we run a loop and count each occurence of the character.

CODE:

x=fileread("filename.txt");

let c be the char whose number of occurrences we wish to find.

n=length(x);

count = 0;

for i=1:n

if(x(i)=='c') count=count+1;

end

end

display(count);

Add a comment
Know the answer?
Add Answer to:
Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...
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 function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Character Count Write a program that reads a file and counts the number of occurrences of...

    Character Count Write a program that reads a file and counts the number of occurrences of each character in the file (case sensitive). The file name is "char.txt". You should use a dictionary to hold the number of occurrences of each character, and print out that dictionary. For example, if the file contains only six characters, "tactic", your dictionary should contain the following key-value pairs (order can be different): {'a': 1, 'c': 2, 'i': 1, 't': 2} Sample Input: Follow...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • 2) Write a function stringManip that takes in a character string and returns a character string...

    2) Write a function stringManip that takes in a character string and returns a character string following these rules: a) any vowel (a, e, i, o, u) is replaced by the character '&' b) any numeric character has 1 added to it (if it was 9, it becomes O instead) (str2num() is useful here) c) all lowercase characters become uppercase d) replace the final character with '!' e) append the length of the string before this step to the end...

  • c# csci312: character counter - vector design a program that reads in an ascii text file...

    c# csci312: character counter - vector design a program that reads in an ascii text file (provided) ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provide... C# CSCI312: Character Counter - Vector Design a program that reads in an ASCII text file (provided) and creates an output file that contains each unique ASCII...

  • python

    Complete the below function that takes a file name and a list as arguments and reads the text within the corresponding file, then creates a dictionary whose keys are the characters of the provided list, and the values are the counts of these characters within the text. If a character in the list is not included in  the text, "0" should be written in the dictionary as the value of this  character. The function returns the created dictionary as a result. If the file  does not exist, then the function returns an empty dictionary. Here is an example case, assuming a file named "sample.txt" exists with the  following content: ----- sample.txt ----- This is an example sentence. This is yet another sentence. ------------------------- >>> text2dict("sample.txt", ['a', 'b', 'c', 't']) {'a':3, 'b':0, 'c':2, 't':4} """ def text2dict(filename, characters):     return # Remove this line to answer this question

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • Write a MATLAB function that counts the number of entries in a vector whose value exceeds...

    Write a MATLAB function that counts the number of entries in a vector whose value exceeds a critical threshold value. The threshold and the vector are arguments in the function.

  • Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array...

    Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array listing words and their counts in sorted order Function Description: Have you ever seen those fancy word-clouds based on the frequency of word occurrences in some text? Well the first step of making a graphic like that is to figure out how often the words in some text occur, which is exactly what this function will do. You will count the number of occurrences...

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