Count how many ‘a’ are in str and eliminate those ‘a’

so for ex in a str= i have an apple
and we want to see how many a and remove them we would use
str1= 'a'
ind = strfind(str, str1)
str(ind) = ''
how would we use a loop for this?

1 Comment

Backup of question in case of edit-delete:
so for ex in a str= i have an apple
and we want to see how many a and remove them we would use
str1= 'a'
ind = strfind(str, str1)
str(ind) = ''
how would we use a loop for this?

Answers (1)

s = 'I have an apple';
[a,b] = regexp(s,'a','split');
numA = length(b);
sentenceWithoutA = strjoin(a);

5 Comments

okk but in a loop i said
Well that seems arbitrary. Silly homework problems. But this technically complies...
for j = 1:1
[a,b] = regexp(s,'a','split');
numA = length(b);
sentenceWithoutA = strjoin(a);
end
Because otherwise it hurts me too much inside to solve such a simple problem in a loop.
@Daniel M: nice loop, made me laugh!
You would probably get a chuckle out of your teacher if you handed in that loop. Also, flagging is not doing what you think. It does attract the attention of site administrators, but it is more likely flags will be dealt with by high reputation members, so exactly the people whose comments you are flagging.

This question is closed.

Tags

Asked:

on 15 Oct 2019

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!