Merge the C code of multiple consecutive spaces in a string.

Source: Internet
Author: User

Merge the C code of multiple consecutive spaces in a string.

1. Problem Description
Combine multiple consecutive spaces in a string into one space. If there is a space at the beginning and end of the merged string, remove it.
For example, "This is a string! "Is a string containing multiple spaces and requires it to be" This is a string !" .

2. C code implementation

/*************************************** * ******************************** All Rights Reserved (C) 2015, Zhou Zhaoxiong. ** File name: CombineSpace. c * File ID: none * Content Abstract: Merge multiple Spaces * Other Description: none * Current version: V1.0 * Author: Zhou Zhaoxiong * completion date: 20150331 *************************************** * *******************************/# include <stdio. h> # include <string. h> // redefine the Data Type typedef unsigned char UINT8; typedef unsigned int UINT32; typedef signed int INT32; // function declaration void CombineSpace (UINT8 * pszOldStr, UINT8 * pszNewStr ); INT32 main ();/****************************** **************************************** * Function description: main function * input parameter: none * output parameter: none * return value: none * Other description: no * modified date version number modifier modified content * found * 20150331 V1.0 Zhou Zhaoxiong create *********************** **************************************** * *******/INT32 main () {UINT8 szOldStr [100] = {0}; UINT8 szNewStr [100] = {0}; // memcpy (szOldStr, "Hello, everyone! ", Sizeof (szOldStr)-1); CombineSpace (szOldStr, szNewStr); printf (" The first case: OldStr = % s, NewStr = % s. \ n ", szOldStr, szNewStr); // memset (szOldStr, 0x00, sizeof (szOldStr); memset (szNewStr, 0x00, sizeof (szNewStr); memcpy (szOldStr, "My name is ZhouZhaoxiong! ", Sizeof (szOldStr)-1); CombineSpace (szOldStr, szNewStr); printf (" The second case: OldStr = % s, NewStr = % s. \ n ", szOldStr, szNewStr); return 0 ;} /*************************************** * ******************************** function description: merge multiple consecutive spaces into one and remove the first space * input parameter: pszOldStr-string before the space is merged * output parameter: pszNewStr-string after the space is merged * return value: no * Other Instructions: No * modified date version number modifier content *-------------------------------------------------------------------* 20150331 V1.0 Zhou Zhaoxiong create *********************************** * **********************************/void CombineSpace (UINT8 * pszOldStr, UINT8 * pszNewStr) {UINT32 iOldIndex = 0; UINT32 iNewIndex = 0; UINT8 chSingleChar = '\ 0'; UINT8 iIsSpace = 0; UINT8 szTmpNewStr [100] = {0 }; if (NULL = pszOldStr) | (NULL = pszNewStr) {printf ("CombineSpace: input parameter (s) is NULL! "); Return;} while ('\ 0 '! = (ChSingleChar = pszOldStr [iOldIndex ++]) {if (chSingleChar = '') // multiple consecutive spaces are reserved for only one {if (iIsSpace = 0) {iIsSpace = 1; szTmpNewStr [iNewIndex ++] = chSingleChar ;}} else {iIsSpace = 0; szTmpNewStr [iNewIndex ++] = chSingleChar ;}} // if the last character is a space, remove it if (szTmpNewStr [strlen (szTmpNewStr)-1] = '') {szTmpNewStr [strlen (szTmpNewStr) -1] = '\ 0';} // if the first character is a space, remove it if (szTmpNewStr [0] = '') {memcpy (pszNewStr, szTmpNewStr + 1, strlen (szTmpNewStr)-1);} else {memcpy (pszNewStr, szTmpNewStr, strlen (szTmpNewStr);} return ;}

3. program running result
Run "gcc-g-o CombineSpace. c" in Linux and then run the "CombineSpace" command. The result is as follows:

The first case: OldStr= Hello,   everyone! , NewStr=Hello, everyone!.The second case: OldStr= My name   is   ZhouZhaoxiong! , NewStr=My name is ZhouZhaoxiong!.

My public account: zhouzxi. Please scan the following QR code:

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.