Hello,
I am doing an online course on matlab in coursera and I have come a cross a problem that seems hard for me to solve.
I need to write a function that takes the name of the text file and a paticular charater as input and then the function must count how many times the character is present in the file and return that number.
function charnum = char_counter(b,a) fid = fopen(b,'r'); if fid < 0 || ~ischar(a) || length(a) == 0 charnum = -1; return end t=0; d=1; while (d>0) d = fgetl(fid); x = strfind(d,a); t = t + length(x); end charnum = t; fclose(fid);end
I have passed 3 out of 4 checks, following is the error that I am getting
Variable charnum has an incorrect value. When testing with ' ' your solution returned 1 which is incorrect. (75444).
I tried countin the number of " ' ' " as well and it seemed correct to,
I would like to the mistake that I am making.
Thanks
Hussain
Best Answer