Delete the blank spaces at the beginning and end of the string, and separate multiple spaces in the middle of the array (if any)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.