Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The problem must be answered using MATLAB. Thanks. Declare a string variable str

ID: 3600528 • Letter: T

Question

The problem must be answered using MATLAB. Thanks.

Declare a string variable str as str = "It is Winter. Write a program which will read each character in the string str and will print out 'It is a letter' if the character is a letter and 'lt is a blank' if the character is a space ( '). Display the total number of letters and the total number of blanks in the string. Also display the indices where there is a space in the string array str. (Use for loop and if-else) The output should look like: It is a letter It is a letter It is a blank It is a letter It is a letter It is a blank It is a letter It is a letter It is a letter It is a letter It is a letter It is a letter Total number of letters in the string: 10 Total number of blanks in the string: 2 The blanks in the string are at indices: 3 6

Explanation / Answer

str = 'It is Winter';
letter_count = 0;
blank_count = 0;
blank_position = [];
n = size(str, 2);
for i = 1:n
if isletter(str(i))
fprintf("It is a letter ");
letter_count = letter_count +1;
else
fprintf("It is a blank ");
blank_count = blank_count +1;
blank_position = [blank_position i];
end
end

fprintf(" ");

fprintf("Total number of letters in the string: %d ", letter_count)
fprintf("Total number of blanks in the string: %d ", blank_count)
fprintf("The blanks in the string are at indices: ")

for i = 1: size(blank_position, 2)
fprintf("%d ", blank_position(i));
end
fprintf(" ");

Sample execution