This code seems to work for any number of spaces either side of the 'x'... as long as the "change" array is long enough, of course...
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char test[]={"blah x blah"},
change[20]={"\0"};
int j, i, len=0, count=0;
printf("Test string was : %s\n\n",test);
len=strlen(test);
for(i=0;i<len;i++)
{
if(test[i]=='x') /* change the 'x' to a space */
test[i]=' ';
}
for(j=0,i=0;i<len;j++,i++)
{
if(test[i]!=' ')
change[j]=test[i];
else
{
count++;
if(count==1) /* copy first space only */
change[j]=test[i];
else
j--;
}
} /* end for */
printf("Changed string is: %s\n\nEnd of program!",change);
return 0;
}
Good night all!!