There are more errors beside those mentioned in solution 1:
for(i=0;str[i]!='\0';++i)
Here you are accessing the
str
variable without having it initialised. The compiler will complain about it when using a high warning level.
You probably want to use the
word
string here instead.
Same for the variable
j
which is never initialised.
Correct code:
int i,y,c,w;
int j = 0;
char str[100],word[1000];
str[0] = '\0';
for(i=0;word[i]!='\0';++i)
The next problem is in this line:
y=strlen(str);
The
strlen
function requires that the passed string is NULL terminated. But your string is not NULL terminated. Instead of using
strlen
you can count the length yourself (when appending characters) or set the following one to NULL:
{
if( word[i]!=' ')
{
str[j] = word[i];
j++;
str[j] = '\0';
}
Note that your
change
function is affected too (the string must be also NULL terminated for comparison). So ensuring that it is always NULL terminated would be the best solution.