#include <stdio.h>
#include <string.h>
#include <errno.h> #define BUF_SIZE 128
/*
* requirement : Processing a string , Remove extra spaces from the string 、'\t' And empty line , And meet the following requirements :
* (1) The original string can only be scanned once .
* (2) Not allowed to apply for new space .( There's a little doubt about this , Declaring variables also applies for space .)
* (3) There can be no spaces at the beginning and end of the processed string 、'\t' And empty line .
* (4) If spaces and '\t', Then the processed string only needs to store a space .
* (5) If spaces appear continuously in the original string 、'\t' And empty line , Then the processed string only needs to store an empty line
* ( I don't understand this empty line clearly. It's a \n, Or two \n?, The last two of the string \n It's going to be one
* A visible blank line , This place could be \n I thought ).
*
* This question took me two hours , Three times .
* summary : 1. Understanding the problem is important .
* 2. Turn the problem into a reasonable model , It can reduce a lot of work .
* 3. Every part is right , It doesn't mean the whole is meaningful .
*
* The processing model of this problem is string copy .
* But there are conditions . That's what it says , The conversion to a program is like this .... Look at the code ,
* All the comments are superfluous .
*
* About bug Why not fix it directly , Because I think people only give functions .
*/ /*
* FIXME: If it's fixed bug Words , This will be changed to
* -----------
* char remove_extra_space(char *str, int is_trim_header)
* -----------
*/
void remove_extra_space(char *str)
{
char *s = str; // Represents the generated string , because s More than a temporary string .
char *prv = NULL; // Represents the last valid character . This effective means ...( You should understand ) for(; prv==NULL ||*prv != '\0'; ++str){ // The advantage of this is Will not forget ++str. and continue It can be executed later .
switch (*str){ case '\t': *str = ' '; // It is convenient to deal with in a unified way . Because the results are all spaces
case ' ' :
/*
* FIXME: If it's fixed bug Words , This will be changed to
* -----------
* if ( (prv == NULL && is_trim_header != 0) || *prv == '\n' || *prv ==' '){
* -----------
*/
if (prv == NULL || *prv == '\n' || *prv ==' '){
continue;
}
break; case '\0':
case '\n':
if (prv == NULL){
/*
* FIXME: If it's fixed bug Words , This will be changed to
* -----------
* if (*str == '\n' && is_trim_header != 0)
* -----------
*/
if (*str == '\n')
continue;
}
else if ( *prv == ' ' || *prv == '\n'){
*prv = *str;
/*
* FIXME: This place gets the return value , Return value initial value '\0'
*/
continue;
}
break;
} if (s != str){
*s = *str;
}
prv = s;
++s;
}
/*
* FIXME: Return return value .
*/
} int main(int argc,char *argv[])
{
char read_buf[BUF_SIZE] = {'\0'};
int read_size = ; errno = ; while ((read_size = fread(read_buf, sizeof (char), BUF_SIZE-, stdin)) >){
read_buf[read_size] = '\0'; /*
* FIXME: Notice that this place has bug, The read file is divided into BUF_SIZE Small pieces ,
* If there's a space in it ( or '\t' ) Division , There will be a space missing .
* If you split before wrapping , The line feed will be lost .
* Solving the case is a int is_trim_header ( Whether to eliminate whitespace or newline at the beginning of the string );
* If is_trim_header Not for 0 be Leave no space at the beginning ,
* by 0 be According to the situation that the space appears in the middle or at the end of the sentence .
*
* Here we need to judge the end of the last line , And whether there are white spaces at the beginning of this line to determine is_trim_header Value .
* We need more here remove_extra_space Go back and read \0 The character that was deleted when , To determine how to compensate
* Space or carriage return
*
*
* if ( What's deleted is \n && This line has content ) compensate \n,is_trim_header = 1;
* else if ( What's deleted is ' ' && This line has content ) is_trim_header = 0;
* else is_trim_header = 1;
*/ remove_extra_space(read_buf); printf("%s",read_buf);
} if (errno != ){
printf(strerror(errno));
} return ;
}

c Language practice __ Remove extra white space _ More related articles revised

  1. 【 Code Notes 】iOS- Floating point number processing and removing the redundant 0

    One , Code . - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...

  2. Birt How to set export format and remove redundant buttons

    1. Set export format : webcontent>birt>pages>dialog>ExportReportDialogFragment.jsp page : find for ( int i = ...

  3. Remove extra white space characters and empty lines from the string (C Language implementation )

    requirement : Processing a string , Remove extra spaces from the string . Horizontal tabs and blank lines , And meet the following requirements : (1) The original string can only be scanned once .(2) Not allowed to apply for new space .(3) There can be no spaces at the beginning and end of the processed string . Tabs and blank lines .(4) ...

  4. .NET Core Remove the extra language pack folder when publishing

    use .NET Core 3.x As the target framework, after publishing , You'll find a lot more language pack folders , Be similar to : occasionally , Don't want to generate these language pack folders , You need to configure it a little bit . stay  PropertyGroup  Add the following ...

  5. DataGridView Remove the extra columns

    Get rid of DataGridView Redundant columns : this.DataGridView.AutoGenerateColumns = false;

  6. magento Of url in Remove redundant directory levels

    Sometimes we need to display only one layer of directory URL route . Instead of the reality of multiple paths , We can modify it in the following ways : Edit  /app/code/core/Mage/Catalog/Model/Url.php find 632 ...

  7. mongodb transformation List Entity ( Remove redundant fields ) Batch addition

    Gson gson = new Gson(); String str = "list aggregate json character string "; JsonObject object = gson.fromJson(st ...

  8. Regular matching Get rid of Redundant js and html label

    $reg17 = '/><strong> Company introduction <\/strong><\/td>([\S\s*]+?)<\/div>/'; $this->d ...

  9. c Language practice __ Find N The sum of the factorials of .

    #include <stdio.h> /* The title is as follows * S = 1 + 2! + 3! + ... + N! */ int main(int argc, char *argv[]) { ...

Random recommendation

  1. linux Lower installation configuration DHCP The server

    The premise is that core And base Two groups 1 2 3 4 5 # cat /etc/redhat-release   Red Hat Enterprise Linux Server relea ...

  2. 【Android】 Another one Fragment Lazy loading ( Load only once )

    effect Old rules , Let's have a look at the renderings you 're right , I'm in the pit again , I made a new one Gank client , Because the previous code was too bad , This time I had a good consideration about the architecture and so on , The code should be easier to read , Well . ha-ha , Welcome again to star ...

  3. python Easy to mix and easy to mess (2)

    Cut strings into lists : With str Slice for separator mystr, If maxsplit There is a specified value , Then just divide maxsplit A string , obtain maxsplit A list of strings Using the string of split() Method >> ...

  4. Spring Annotated BeanPostProcessor And InitializingBean

    /*** BeanPostProcessor For each bean Provide personalized modification when instantiating , Do some packaging, etc */ package org.springframework.beans.factory.config; ...

  5. 『Sklearn』 Feature vectorization processing

    『Kaggle』 Classification task _ Decision tree & Integrated model &DataFrame Vectorization operation 1 2 3 4 5 6 7 8 9 ''' Feature extractor ''' from sklearn.feature_extr ...

  6. clientX/Y pageX/Y offsetX/Y layerX/Y screenX/Y clientHeight innerWidth...

    About js Mouse events synthesize the attributes that major browsers can get coordinates. There are five kinds in total event.clientX/Y event.pageX/Y event.offsetX/Y event.layerX/Y event.sc ...

  7. 1.ehcache Realize the whole page cache and local page cache

    from :https://www.cnblogs.com/jianjianyang/p/4933016.html I haven't blogged for a long time , I really don't have time .ps: It's just laziness !! The next few articles are about caching , It's mainly about ...

  8. To configure mysql Allow remote Links

    By default ,mysql The account is not allowed to log in remotely , Only in localhost Sign in . This article provides two ways to set mysql You can connect through a remote host . Modify the data in the user table Log in mysql after , change mysql In the database ...

  9. Js/Jquery Get the height of the visible area of the web screen

    Get the height and width of the viewing area of the browser window , Scroll bar height, friends in need can refer to . 1 document.body.clientWidth ==> BODY Object width 2 document.body.clie ...

  10. Use Tomcat+Redis To implement the Session Sharing issues

    One . At work, because we need to use Tomcat Cluster deployment , It's about Session Sharing issues , There are three main solutions : 1. Use a database to store Session 2. Use Cookie To store Session 3. Use Redis ...