Originally Posted by
Matticus
You're attempting to modify a string literal, which is a no-no. Using "temp" as an intermediary pointer just hides this fact.
Try changing "line" to a suitably sized array and see what happens.
I suspect you did not come up with an algorithm before writing this code, since the results are incorrect. I suggest planning this out on paper before trying to implement it in code.
Hi Matt,
Thanks for your reply.
Code:
unsigned int j=0;
char line[]="a good";
char *temp;
while(*(line+j)){
if(*(line+j)==' ' && *(line+j+1) ==' ' ){
temp=line+j+1;
while((*temp++)!='\0'){
*(temp-1)=*(temp);
}
}
else{
j++;
}
}
line[j]='\0';
If I create a function, and pass a string literal then it would fail too, I believe. Because that is my next step.