How to do a backspace processing for string in C? Here is a simple example of what I wrote
char buf[1024]="D,e,Bs,a,t,e" \\*Bs means Backspace
char tmp[1024];
int j,n;
n=0;
sprintf(tmp,"%s",buf);
for(j=0;tmp[j] !='\0';j++)
{
if ((tmp[j] == '\x08') || (tmp[j] == 127))
{
j++;
n--;
}
buf[n] = tmp[j];
n++;
}
buf[n] = '\0';
printf("%s",buf);
This will print = Date
But if the buf have more than 1 Bs, example
char buf[1024] = "D,e,e,Bs,Bs,a,t,e"
The output will be = DeBsate
The function will find only 1 backspace. Why? Because the j are increased after it process the first Bs and ignored checking the Bs after that. Of course, if check on terminal, the output are still date. But if I log the debug just to see the string inside buf. The second Bs will still be there. Which is not suppose to. How do I modify this function?