Calculate vowels consonants spaces numbers in a sentence
Show older comments
clc
close all
cons=0;
vowels=0;
spaces=0;
num=0;
sp=0;
strg=input('Given string is : ','s');
l=strlength(strg);
for i=1:l
if strg(i)=='a' || strg(i)=='e' || strg(i)=='i' || strg(i)=='o' || strg(i)=='u' strg(i)=='A' || strg(i)=='E' || strg(i)=='I' || strg(i)=='O' || strg(i)=='U'
vowels=vowels+1;
elseif (strg(i)>= 'a' && strg(i)<='z') || (strg(i)>= 'A' && strg(i)<='Z')
cons=cons+1;
elseif strg(i)==' '
spaces=spaces+1;
elseif strg(i)>=0 && strg(i)<=9
num=num+1;
else
sp=sp+1;
end
end
fprintf('Number of vowels=%d',vowels);
fprintf('Number of consonants=%d',cons);
fprintf('Number of spaces=%d',spaces);
fprintf('Number of numbers=%d',num);
This is my code and I am getting an error at line 11 . Can anyone please help me out and check if there are any other errors?
Accepted Answer
More Answers (2)
SC
on 30 Jun 2020
1 vote
in Matlab, array indices start from '1'
please edit (line:10)
1 Comment
Aagam Jain
on 30 Jun 2020
Vashist Hegde
on 30 Jun 2020
1 vote
In MATLAB, indexing always starts from 1, change the indexing on line:10 to for i = 1:l
1 Comment
Aagam Jain
on 30 Jun 2020
Categories
Find more on Startup and Shutdown 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!