Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

ASSIGNMENT: TEXT FILES 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.

2 visualizaciones (últimos 30 días)
When testing with '' your solution returned -1 which is incorrect. (0)
This error pops up while doing the assignment.
can anyone tell me why is this error popping and the meaning of the statement mentioned above.
when the character is ' " ' then i get a finite answer. and when the character is ' ' ' then too answer is a finite number. but i dont understand what the above bold text means....
my code
function charnum = char_counter(fname,character)
fid=fopen(fname);
if fid< 0
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 =-1;
return;
end
  3 comentarios
Rakshith R
Rakshith R el 30 de Mzo. de 2019
thanks for clearing this
but when i remove those statement i get a error stating
char_counter('simple.txt',11) failed
why so
Shubham Pandey
Shubham Pandey el 11 de Abr. de 2020
Editada: Shubham Pandey el 11 de Abr. de 2020
The basic error this code is generating is that when you pass '' as character and if it reads 0 and stores it in charnum, this charnum is getting changed to -1 due to last if block. That is why, the error is showing up. Therefore, it is advisable to remove the last if block. Instead, you may add additional OR condition in the first if block to check for the validity of the character in addition to fid<0.

Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Mzo. de 2019
Editada: Walter Roberson el 29 de Mzo. de 2019
You are apparently using some kind of automated assignment assessing software. You submitted an assignment for grading. The course software tried passing '' (the empty character vector, equivalent to char([]) ) to your function, and your function returned the value -1 which is not what the assignment specifies should be returned for that input, so it told you that you are wrong.
We do not know what assignment you were doing, or what the correct answer was to that input. You will need to re-read the question more carefully to see what you should have responded with.
I speculate that you were doing the assignment that has been going around about counting the number of matches of a character in a file. The assignment other people have been posting requires that -1 be returned if the input is invalid. I suspect that you are seeing the '' (empty character vector) and considering it to be invalid and so returning -1, but that if you were to read the assignment more carefully you would find that the assignment considers '' to be a valid input and that the number of matches you would expect for it would be 0. Be careful with your validation tests: isempty() is true for both [] and '' .

Más respuestas (4)

Jaimin Motavar
Jaimin Motavar el 2 de Jul. de 2019
can some tell me what is the my mistake in this code?problem is written in comment secssion.
function charnum = char_counter(filename,b)
charnum=0;
if ~ischar(b)
charnum=-1;
return;
end
fid = fopen(filename,'rt');
if fid<0
charnum=-1;
return;
end
oneline= fgets(fid);
while ischar(oneline)
a=sprintf('%s \n',oneline);
c=findstr(b,a);
[m,n]=size(c);
charnum=charnum+n;
oneline=fgets(fid);
end
end
  3 comentarios

Ajith Thomas
Ajith Thomas el 19 de Ag. de 2019
Editada: Ajith Thomas el 19 de Ag. de 2019
my question is which all characters should be eliminated? on this question?
  2 comentarios
Walter Roberson
Walter Roberson el 19 de Ag. de 2019
There are different versions of the assignment around. In the most common case, there are no characters that should be eliminated. The tests against Q V and so on were hacks to deal code mistakes the user had made.
A common mistake people make is that they are returning -1 when a valid input is not found. They should be returning 0 instead. -1 is reserved for file not found or invalid input argument.

Preethi Vannal
Preethi Vannal el 12 de Abr. de 2020
I passed 3 assessments out of 4 for this same question.
My code:
function charnum = char_counter(fname,ch)
if isfile(fname)
fid=fopen(fname,'rt')
if (fid>0 && ischar(ch))
ct=0;
oneline=fgets(fid);
while ischar(oneline)
if(strfind(oneline,ch))
ct=ct+count(oneline,ch);
else
charnum=-1;
end
oneline=fgets(fid);
end
charnum=ct;
else
charnum=-1;
end
else
charnum=0;
end
fclose(fid);
end
I am not able to get idea to pass this 4th assessment.
Can anyone please help me?
  4 comentarios
Mohymen Kushal
Mohymen Kushal el 2 de Nov. de 2020
Don't close the file. fopen will return -1 if the file dosen't exist. But if you close it with fclose it won't.
Walter Roberson
Walter Roberson el 2 de Nov. de 2020
It is good practice to fclose() any file you fopen() unless you are returning the handle of the file (or recording it internally.)
However, the code posted attempts to fclose() the file even if the exist() test failed, which is a problem because if exist() failed then the variable fid would not have been assigned to.

tshering lhamo
tshering lhamo el 5 de Sept. de 2020
Editada: Rik el 5 de Sept. de 2020
hello, my function gives the error "test with all visible characters-failed". please help me understand where I made a mistake. thank you!
function charnum=char_counter(fname,character)
fid=fopen(fname,'rt');
if ~ischar(character)||fid<0
fprintf('error opening file\n');
charnum=-1;
return;
else
fname=fgets(fid);
charnum=0;
for ii=1:length(fname)
char_n=count(fname(ii),character);
charnum=charnum+char_n;
end
end
fclose(fid);
  1 comentario
Rik
Rik el 5 de Sept. de 2020
You don't close the file if the character input is invalid, and you only read a single line. Also, as you're going character by character you don't really need the count function. And why do you store the file contents in fname? That name doesn't describe the contents at all.

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by