I am trying to convert a string with spaces into an array of char without spaces.
Here is what I tried
cout << "Enter a string: ";
for (int a=0;a<=TempNumOne;a++)
Enter a string: hello world
This is because:
if(str[a]!=' ') Filename[a]=str[a];
In "hello world",
w. The above code will: store the
Filename and the
Filename, instead of
Filename. A computer only does what you tell it to do, and now what you want it to do. Your logic did skip over the space, but all it did is that it didn't copy it into the corresponding position into the
Filename buffer. This is not enough to accomplish the given task.
To do this correctly you need to maintain a separate index variable, call it
b, for example, initialize it to 0, then:
if(str[a]!=' ') Filename[b++]=str[a];
and don't forget to null-terminate the
Filename, at the end.