Dec 19, 2012 at 10:42am UTC
The program below shows a bizarre disfunction. Its target is to delete all empty fields inside a string. I would appreciate any advice about how to make it less complicated too.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void DeleteSpace(char A[])
{
int i,j,m,count;
for (i=0;i<strlen(A);i++)
if (A[i]==' ')
{ j=i;
while (j<strlen(A))
{
A[j]=A[j+1];
if (A[j+1]==' ')
{
count=2;
m=j+2;
do
{
if (A[m]==' ')
{count++;
m++;}
else break;
} while (A[m]!='\0');
for (i=j;i<=strlen(A);i++)
A[i]=A[i+count];
}
j++;
}
}
printf("%s\n",A);
}
main()
{
char C[10];
gets(C);
DeleteSpace(C);
system("pause");
}
Last edited on Dec 19, 2012 at 10:42am UTC
Dec 19, 2012 at 3:05pm UTC
the problem occurs when you detect a second space. Don't do any special thing then just prevent
i
from being increased.
Oh, and:
Please use code tags: [co
de]
Your code
[/co
de]
See:
http://www.cplusplus.com/articles/z13hAqkS/
Last edited on Dec 19, 2012 at 3:06pm UTC