[C] - String Manipulation {Beginner Programmer}
Hello.
I have an assignment where I need to clean up a string, removing tabs, new lines, leading and trailing whitespace, yet leave proper spaces between words.
So far I have been able to remove newlines and tabs, and replace them with a single space.
Code:
char s[] = " widget;Acme \t \n Co.;gear induction\b\tdevice: \n "
Code:
int cleanSpace(char s[])
{
int i, j;
for(i = 0; s[i]; i++)
{
if(s[i] == '\t' || s[i] == '\n')
{
s[i] = ' ';
}
}
}
This will now output,
Code:
" widget;Acme Co.;gear inductio device: ".
Now that the newlines and tabs are gone, I need to remove the leading, trailing and multiple whitespace in the middle.
I am confused as to how I can approach this.
My professor insisted that I do not use ctype.h (isspace), that I work with my given function header, I do not use pointers or second buffers.
He hinted that I should use 4 loops, with the first doing what I posted above. He hinted that I should find multiple whitespace, find the next non space and then copy everything down a spot.
He insisted that I break it down, as I have above. Starting with removing new lines/tabs and replacing them with a space.
Any help strongly appreciated.
Thank you in advance.