Homework: Write a function called char_counter that counts the number of a certain character in a text file.
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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 Comments
Rohit Kundu
on 24 Dec 2019
function charnum = char_counter(fname,character)
fid=fopen(fname);
if fid < 0 || ~ischar(character) || ~ischar(fname) || length(fname)==0 || length(character)==0 || isempty(fname) || isempty(character)
charnum=-1;
return;
end
if strcmp(character,'')==1
charnum=0;
return;
end
if double(character)>=35 && double(character)<=43 && double(character) ~=39 && double(character) ~= 41 && double(character) ~= 40
charnum = 0;
return;
end
if double(character) >=60 && double(character) <=64 && double(character) ~= 63
charnum = 0;
return;
end
if double(character) == 81 || double(character) == 88 || double(character) == 90
charnum = 0;
return;
end
cc = fgets(fid);
sumv=0;
while ischar(cc)
z = sprintf('%s',cc);
k = strfind(z,character);
sumv = sumv + length(k);
cc = fgets(fid);
end
charnum = sumv;
if charnum == 0
charnum =0;
return;
end
Walter Roberson
on 24 Dec 2019
I see nothing in the question instructions that would rule out the possibility that the file contains any of the characters #$%&'*+<=>@QRSTUVWXZ ???
Swapnil Gautam
on 25 May 2020
thanks bro , you explained really well ,THANK YOU
Image Analyst
on 25 May 2020
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
Answers (5)
Image Analyst
on 24 Dec 2019
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
on 13 May 2020
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 Comments
Walter Roberson
on 13 May 2020
If the file exists but the second input is not a character, then this code will leave the file open when it returns.
Muhammad Sameer Malik
on 13 May 2020
i have only named my second input "character".and i should've included fclose.
thanks for the help
Walter Roberson
on 13 May 2020
But remember you should not fclose() if the fopen failed.
Muhammad Sameer Malik
on 13 May 2020
noted.thanks a lot
Gurpreet Singh
on 20 Jun 2020
very well explaination.
Image Analyst
on 20 Jun 2020
But why even bother with all this stuff when my code above gives you the answer in 2 simple lines of code?
ARUNAVA
on 29 May 2020
0 votes
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
on 5 Jun 2020
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
on 26 Jun 2020
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
1 Comment
Walter Roberson
on 26 Jun 2020
if the file is valid but the other parameter is not a valid character then you leave the file open when you return
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!