Question: Delete the blank spaces at the beginning and end of the string, and separate multiple spaces in the middle of the array (if any)
Code:
Void delspace (char * s) {If (S = NULL) return; int flag = 0; If (* s = '') Flag = 1; char * P = s; Int J = 0; while (* P! = '\ 0') {If (* P! = '') S [J ++] = * P; else {While (* P ='') P ++; if (flag = 1 | * P = '\ 0') Flag = 0; elses [J ++] = ''; p --;} p ++ ;} s [J] = '\ 0 ';}
Note the constraints of function calling in the above Code.
From the code, we can see that no new space is allocated in the function, and no extra space is referenced in the parameter list, all the changes are modified based on the original string, so pay attention to this point, because the string Declaration has two methods.
// Character array char STR [] = "Hello world! "
The other is a String constant.
// String constant char * STR = "Hello world! "
However, the preceding two strings have obvious differences. The character array is a string variable. You can call the preceding delspace () function to directly modify the variable space, but the String constant is a constant, constants cannot be modified. Therefore, if you declare a String constant, calling the preceding function to delete spaces will result in
Unhandled exception in cmd.exe: 0xc0000005: access violation. This is mainly caused by the error of modifying the String constant.
Therefore, if no space is allocated, direct modification only applies to string arrays. It cannot be used as a String constant.