I know that you want to write a loop to remove a few spaces between words, but the best way to remove the white space in your specific problem is to use regular expressions , in particular regexprep . Regular expressions are used to search for specific patterns / substrings in a larger string. In this case, we are trying to find substrings consisting of more than one space. regexprep finds substrings matching the pattern and replaces them with another string. In our case, you will search for any substrings inside your string containing at least one more whitespace character, and replace them with one space character. Also, I see that you cut off both leading and trailing spaces for the string using strtrim , which is great. Now all you have to do is call regexprep like this:
Word = regexprep(Word, '\s+', ' ');
\s+ is a regular expression for finding at least one space character . Then we replace this with a single space. Thus, suppose we saved this line in Word :
Word = ' hello how are you ';
Doing trimming of leading and regexprep spaces, and then calling regexprep in what we said gives:
Word = strtrim(Word); Word = regexprep(Word, '\s+', ' ') Word = hello how are you
As you can see, the start and end white spaces were deleted using strtrim , and the regex will take care of all the other spaces between them.
However, if you are configured to use a loop, you can use the logical variable, which is set to true when we find a space, and then we use this variable and skip another white space until we get into a character that is not space. Then we would place our space, then / , then a space, then continue. In other words, do something like this:
Word = strtrim(Word); %// Remove leading and trailing whitespace space_hit = false; %// Initialize space encountered flag Word_noSpace = []; %// Will store our new string for index=1:length(Word) %// For each character in our word if Word(index) == ' ' %// If we hit a space if space_hit %// Check to see if we have already hit a space continue; %// Continue if we have else Word_noSpace = [Word_noSpace ' ']; %// If not, add a space, then set the flag space_hit = true; end else space_hit = false; %// When we finally hit a non-space, set back to false Word_noSpace = [Word_noSpace Word(index)]; %// Keep appending characters end end Word = Word_noSpace; %// Replace to make compatible with the rest of your code for Character = Word %// Your code begins here ... ...
What the above code does is that we have an empty line called Word_noSpace that will contain our word without extra spaces, and these spaces are replaced by a single space. A loop goes through each character, and if we encounter a space, we check to see if we have encountered space. If we have, just keep looping. If we have not done this, combine the space. As soon as we finally hit a non-spatial character, we just add those characters that are not spaces for this new line. The result will be a string without extra spaces, and they will be replaced with a single space.
Execution of the above code after aligning the top and back spaces gives:
Word = hello how are you