how to count repeated character in string ??

30 次查看(过去 30 天)
string loke this aaaaddccccfeeee how to count the repeating of character (a) a=3

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2014-3-29
编辑:Azzi Abdelmalek 2014-3-29
str='aaaaddccccfeeee'
[ii,jj,kk]=unique(str)
freq=histc(kk,1:numel(ii))
c=regexp(ii,'.','match')'
out=[c num2cell(freq)]

更多回答(2 个)

Jan
Jan 2014-3-29

Hassaan
Hassaan 2023-12-22
To count the occurrences of a specific character in a string in MATLAB, you can use the count function.
str = 'aaaaaddddccceeeeee'; % The string to search
characterToCount = 'a'; % The character you want to count
% Count the occurrences
occurrences = count(str, characterToCount);
% Display the result
fprintf('The character "%s" appears %d times in the string.\n', characterToCount, occurrences);
Output
The character "a" appears 5 times in the string

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by