Since you are trying, here is the problem. Input hello world...
I am going to increase the pointers - in your function- no matter what the current character is. So everything works as we want until we meet the first whitespace.
The if condition will be false, thus we won't copy anything, but we will increase the pointers. We then continue our job as intended, until we find the next whitespace or end. Here there is no whitespace.
So str_buf will be like this
|h|e|l|l|o|nothingAssignedByTheProgrammerHere|w|o| ....
So, why doesn't the printf outputs helloJUNKworld?
First idea I had was: Because it has been initialized to null terminators, thus the cell that has been untouched by you has a null terminator, thus printing ends there.!
Despite the fact that malloc does not guarantee something like that
Code:
The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
Moreover, local initialized variables do not have a standard value. But in my pc and yours you see what happens. They have the null terminator before getting a value by you.
Just after the malloc I did this
Code:
for(i=0;i<line+1;i++)
if(str_buf[i]=='\0') printf("%d",i);
and here is my output
which means that null terminators are all over the place.
Then in main, I did this
Code:
l = strlen(new_string);
for(i=0;i<l+1;i++)
printf("\nnew string in main: %c %d\n", new_string[i], l);
and this gave me output
Code:
new string in main: h 5
new string in main: e 5
new string in main: l 5
new string in main: l 5
new string in main: o 5
new string in main: 5 <-- HERE I HAVE THE NULL TERMINATOR
which actually gives base to somebody to say that all I said before is just not correct. Well, the truth is that strlen will stop at the null terminator (the cell that we left untouched...). And of course, that is what she should do, as strings in C are to be terminated with the null terminator! So, strlen supposes that when it reaches the first null terminator, the string is terminated.
So, if I do this
Code:
for(i=0;i<13;i++)
printf("\nnew string in main: %c %d\n", new_string[i], l);
I will get as output this
Code:
new string in main: h 5
new string in main: e 5
new string in main: l 5
new string in main: l 5
new string in main: o 5
new string in main: 5 <--- NULL TERMINATOR
new string in main: w 5
new string in main: o 5
new string in main: r 5
new string in main: l 5
new string in main: d 5
new string in main: <--- NEWLINE CHARACTER. Remember, we used fgets!
5
new string in main: 5 <--- NULL TERMINATOR
Simple fix
Code:
if(*given_string !=' '){
*str_buf = *given_string;
printf("%c", *str_buf);
}
else{
word_len--;
str_buf--;
}
But if I were you, I would just write
Code:
while(*given_string){
if(*given_string !=' '){
*str_buf = *given_string;
str_buf++;
word_len++;
printf("%c", *str_buf);
}
given_string++;
}
Do not do moves that do not need to be done. To your work with always the minimum steps and lines of code (as long as they remain readable of course :P )
Hope this helps