Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

2 次查看(过去 30 天)
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.
function charnum = char_counter(fname,character)
fid = fopen(fname,'rt');
if fid < 0 || ~ischar(character) || ~ischar(fname) || length(fname)==0 || length(character)==0 || isempty(fname) || isempty(character)
charnum=-1;
else
l=[];
oneline = fgets(fid);
while ischar(oneline)
l=oneline;;
oneline = fgets(fid);
end
z=count(l,character);
if z>0
charnum=z
else
charnum=-1;
end
end
I'm getting an error for test for all visible characters.
Assessment result: incorrectTest with all visible characters
Variable charnum has an incorrect value.
When testing with ' ' your solution returned 1 which is incorrect. (75444)
  4 个评论
Image Analyst
Image Analyst 2020-5-25
I still think my answer is better:
charnum = char_counter('textData.txt', 'Ekt')
function charnum = char_counter(fname, character)
str = fileread(fname); % Read entire file into a string.
% Count the number of EACH specified character in the file.
charnum = zeros(1, length(character));
for k = 1 : length(character)
charnum(k) = sum(str == character(k));
fprintf('Found %d %c.\n', charnum(k), character(k));
end
end

回答(5 个)

Image Analyst
Image Analyst 2019-12-24
Hint:
str = fileread('whatever.txt') % Read entire file into a string.
% Count the number of L's in the file
count = sum(str == 'L')

Muhammad Sameer Malik
function charnum=char_counter(fname,character)
charnum=0;
fid=fopen(fname,"rt");%opens file
if fid<0 || ~ischar(character) %check if file not found or character is not a char
charnum=-1;
return
end
oneline=fgets(fid);
while ischar(oneline)
for x=1:length(oneline)
%compare whether 'character' and words from line are same
if strcmp(oneline(x),character)==true
charnum=charnum+1;
end
end
oneline=fgets(fid);
end
  6 个评论

ARUNAVA
ARUNAVA 2020-5-29
function charnum=char_counter(fname,character)
fid=fopen(fname,'rt');
if fid<0 || ~ischar(character)
charnum=-1;
return;
end
x=fscanf(fid,'%c');
charnum=length(find(x==character));
end

Randall Ang
Randall Ang 2020-6-5
This is my solution, u can take a look for other alternatives.
function charnum = char_counter(fname,character)
%fname - char vector of the filename
%character - char it counts in the file
%charnum - number of characters found
%if file is not found / character is not valid, charnum = -1
fid = fopen(fname, 'rt'); %open file to read text file
if fid>0 & ischar(character)
fline = fgets(fid);
k = 0;
while ischar(fline)
for i = 1:length(fline)
if fline(i) == character
k = k + 1;
end
end
fline = fgets(fid);
end
charnum = k;
else
charnum = -1;
end

Sakib Javed
Sakib Javed 2020-6-26
function charnum = char_counter(fname,character)
fid = fopen(fname,'rt');
if fid < 0 || ~ischar(character)
charnum = -1;
return
end
oneline = fgets(fid);
n = 0;
while ischar(oneline)
for ii = 1:length(oneline)
if oneline(ii) == character
n = n+1;
end
end
oneline = fgets(fid);
end
charnum = n;
fclose(fid);
end

此问题已关闭。

产品


版本

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by