i need to create a function that will accept a directory path. But in order for the compiler to read backslash in i need to create a function that will make a one backslash
int arrlength = sizeof(newpath);
causes the size of your entire array (in char
s) to be assigned to arrlength
. This means you are iterating over 99999 characters in the array, even if the path is shorter (which it probably is).
Your loop condition also allows goes one past the bounds of the array (since the last (99999th) element is actually at index 99998, not 99999 -- arrays are zero-based):
for (int i = 0; newpath[i]] != '\0'; i++)
Also, there is no reason to copy the string into a character array first, when you can loop over the string
object directly.
In any case, there is no need to escape backslashes from user input. The backslash is a single character like any other; it is only special when embedded in string literals in your code.