how to count for whitespace symbol occurrences in a .txt file?
7 views (last 30 days)
Show older comments
i found a way to read the .txt file and count the symbols using this code:
v=textread('FF7.m','%c'); count=length(strfind(v,'(symbol)'));
but i cant find a way to count the whitespaces, space, breaks, or tabs. in addition, since the input should be in script, i cant put the single quotation in the script. i also need to count the ' symbol.
can someone help me? any help will be very appreciated. thank you in advance!
0 Comments
Accepted Answer
Cedric
on 9 Mar 2013
Edited: Cedric
on 9 Mar 2013
Alternatively, you can use a simple regexp. It will certainly not be the most efficient solution though for this simple task:
content = fileread('FF7.m') ;
count = numel(regexp(content, '\s')) ;
EDIT: for counting ' only, you can do
count = sum(content == '''') ;
If you wanted to count/sum whitespaces and ' with a single regexp call, you could do the following:
count = numel(regexp(content, '[\s'']')) ;
0 Comments
More Answers (1)
Walter Roberson
on 9 Mar 2013
'''' is a quoted single-quote.
You may wish to consider using ismember().
Also,
newline = sprintf('\n');
cr = sprintf('\r');
tab = sprintf('\t');
0 Comments
See Also
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!