Calculate vowels consonants spaces numbers in a sentence

17 ビュー (過去 30 日間)
Aagam Jain
Aagam Jain 2020 年 6 月 30 日
コメント済み: Gurpreet Singh 2020 年 7 月 1 日
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?

採用された回答

Gurpreet Singh
Gurpreet Singh 2020 年 6 月 30 日
Hi Aagam
It is my understanding that you are trying to count the number of vowels, consonants, spaces, and numbers in a given string. However, you're getting the error.
This error is occurring because of line 11. There should be an OR (||) operator in-between " strg(i)=='u' " and " strg(i)=='A' ".
Please refer to the https://in.mathworks.com/help/matlab/logical-operations.html (logical operator) documentation for more information on how to use logical operators.
  2 件のコメント
Aagam Jain
Aagam Jain 2020 年 7 月 1 日
Such a silly mistake anyway Thanks
Gurpreet Singh
Gurpreet Singh 2020 年 7 月 1 日
my pleasure!

サインインしてコメントする。

その他の回答 (2 件)

SC
SC 2020 年 6 月 30 日
in Matlab, array indices start from '1'
please edit (line:10)
  1 件のコメント
Aagam Jain
Aagam Jain 2020 年 6 月 30 日
I already tried that it isn't working

サインインしてコメントする。


Vashist Hegde
Vashist Hegde 2020 年 6 月 30 日

In MATLAB, indexing always starts from 1, change the indexing on line:10 to for i = 1:l

  1 件のコメント
Aagam Jain
Aagam Jain 2020 年 6 月 30 日
I already tried that it isn't working

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by