1

I'm trying to make an algorithm in Matlab that scans the character array from left to right and if it encounters a space, it should do nothing, but if it encounters 2 consecutive spaces, it should start printing the remaining quantities of array from next line. for example,

inpuut='a bc  d';

after applying this algorithm, the final output should have to be:

a bc

d

but this algorithm is giving me the output as:

a bc

d d

Also, if someone has got a more simpler algorithm to do this task, do help me please :)

m=1; t=1;
inpuut='a bc  d';

while(m<=(length(inpuut)))

    if((inpuut(m)==' ')&&(inpuut(m+1)==' '))
        n=m;
        fprintf(inpuut(t:(n-1)));
        fprintf('\n');
        t=m+2;
    end    

    fprintf(inpuut(t));

    if(t<length(inpuut))
        t=t+1;
    elseif(t==length(inpuut))
        t=t-1;
    else
    end

    m=m+1;

end
fprintf('\n');
1
  • @Yvon, i've an array containing text, i want to scan it from left to right just as strtok command of matlab, and display the characters; but i want that if it encounters 2 consecutive spaces while scanning from left to right, it should start printing the remaining characters from next line. Commented Aug 11, 2014 at 21:58

3 Answers 3

4

OK I gave up telling why your code doesn't work. This is a working one.

inpuut='a bc  d  ';

% remove trailing space
while (inpuut(end)==' ')
    inpuut(end)=[];
end

str = regexp(inpuut, '  ', 'split');
for ii = 1:length(str)
    fprintf('%s\n', str{ii});
end

regexp with 'split' option splits the string into a cell array, with delimiter defined in the matching expression.

fprintf is capable of handling complicated strings, much more than printing a single string.

You can remove the trailing space before printing, or do it inside the loop (check if the last cell is empty, but it's more costly).

Sign up to request clarification or add additional context in comments.

5 Comments

in which part of the algorithm, i've to make this change? i also know that my code is failing when it encounters one or more spaces in the end, the problem is that i'm not able to fix it :( i know these are a bit foolish sort of questions to ask, but as i'm very new to matlab, so i don't have that much idea of it :(
@Yvon - I laughed out loud when I read your first statement. That alone gave you a +1. However, good job in fixing the code :)
@user3801619 - Replace all of your code with the one Yvon has given you.
thank u so very much @Yvon :) its working fine now (Y)
+1 although you could also use deblank to get rid of trailing whitespace.
2

You can use regexprep to replace two consecutive spaces by a line feed:

result_string = regexprep(inpuut, '  ', '\n');

If you need to remove trailing spaces: use this first:

result_string = regexprep(inpuut, '  $', '');

Comments

1

I have a solution without using regex, but I assumed you wanted to print on 2 lines maximum.

Example: with 'a b c hello':

a b
c  hello

and not:

a b
c
hello

In any case, here is the code:

inpuut = 'a b  c';
while(length(inpuut) > 2)
    % Read the next 2 character
    first2char = inpuut(1:2);
    switch(first2char)
        case '  ' % 2 white spaces
            % we add a new line and print the rest of the input
            fprintf('\n%s', inpuut(3:end));
            inpuut = [];
        otherwise % not 2 white spaces
            % Just print one character
            fprintf('%s', inpuut(1))
            inpuut(1) = [];
    end    
end
fprintf('%s\n', inpuut);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.