Im doing a practice question and I got the question:
Replace any occurrence of two or more consecutive blank spaces with one (single) blank space
I attempted it a bunch of ways but so far the code Ive got is:
str = input('Enter a string: ');[m, n] = size(str);C = 0;for i=1:n if str(i) == ' ' C = C+1; blankpos(C) = i; endendfprintf('Position of the blank spaces: \n');blankposC1 = 0;strm=str;for i=1:n if str == ' ' C1 = C1+1; posreplace(C1)=i; if str == ' ' C1 = C1+1; posreplace(C1)=i; end endendstrm(posreplace) = ' ';fprintf('The modified string: ');strm
Best Answer