Question

Write a function called char_counter that counts the number of a certain character in a text...

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 Please i need the MATLAB code for this question

Homework Answers

Answer #1

MATLAB Script:

close all
clear
clc

charnum = char_counter('simple.txt', 'a')
charnum = char_counter('simple.txt', '^')
charnum = char_counter('simple.txt', ' ')
charnum = char_counter('simple.txt', 'A')

function charnum = char_counter(filename, char)
fid = fopen(filename, 'r');
if fid == -1 || ~ischar(char)
charnum = -1;
return
end
data = fscanf(fid, '%c');
charnum = length(find(data == char));
end

Output:

charnum =
3
charnum =
0
charnum =
5
charnum =
0

File 'simple.txt':

This file should have
exactly three a-s...

Know the answer?
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
Similar Questions
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Write a function remove elt(v,a) that takes as input a vector v and a number a,...
Write a function remove elt(v,a) that takes as input a vector v and a number a, and as output returns a vector that is the same as v but with a single value of a removed. For example, if we run remove elt([-2 1 3 1 5], 1 ), then the output should be either [-2 3 1 5] or [-2 1 3 5]. matlab question
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens...
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens the file for reading Reads an integer from the file and returns it. Create a main function that Asks the user to input a file Passes that file to the “ReadFile” function Prints the returned integer to the screen Extra Credit: Set a default argument of “intfile.txt” for the “ReadFile” function Write an overloaded function of “ReadFile” that accepts two filenames and reads an...
In Python: This will require you to write several functions, and then use them in a...
In Python: This will require you to write several functions, and then use them in a program. Logical Calculator The logical calculator does math, but with logical operators. In logic, we represent a bit with 0 as false and a bit with 1 as true. The logical operators are NOT, AND and OR. Bitwise logical calculations operate on each bit of the input. The NOT operator works on just one three-bit argument. NOT 011 = 100 The AND operator works...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
Using C programming Create a function called printMenu( ) with the following properties: Has no function...
Using C programming Create a function called printMenu( ) with the following properties: Has no function inputs or output. Prints the following menu to the screen: 1. Enter user name. 2. Enter scores. 3. Display average score. 4. Display summary. 5. Quit Create a function called printLine( ) with the following properties: Takes as input a char Takes as input an integer corresponding to the number of times to print the character Has no function output. For example, if we...
Programming in C language (not C++) Write a main function with a function call to a...
Programming in C language (not C++) Write a main function with a function call to a function called Calculation which has two integer arguments/ parameters. The function returns a character. Declare and initialize the necessary data variables and assign balues needed to make it executable and to prevent loss of information. //input 2 integer values //returns the characterequivalent of the higher of the two integer values char Calculation(int, int);
IN MATLAB: Write a function file that takes a vector as an input and returns another...
IN MATLAB: Write a function file that takes a vector as an input and returns another vector with all repeated elements of the original vector. For example, the vector [3 4 1 0 4 -5 7 3] would return the vector [3 4]. Do not use the built-in function "repelem."
Write a function that accepts a line of text and a single letter as input (case...
Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the first character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter. Example: Input text = "When the SUN rises at dawn, the chicken flies into the window." Input letter = "t" Output = "The letter t has appeared 3...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT