Remove extra spaces in the string

Title description

Format strings, delete words and words, letters and letters, extra spaces between words and letters
For example:
"fdfeasdsaf vsdrwf c dfefa adqdw"
after formatting:
"fdfeasdsaf vsdrwf c dfefa adqdw"

Code section

#include <stdio.h>
void deblank(char s[]);

int main(void)
{
    char s[100];    //可以改成动态分配方法,更具普遍性
    printf("please input one strings:");
    fgets(s,100,stdin);
    deblank(s);
    return 0;
}

void deblank(char s[])
{
    printf("after deblank:");
    int point=0,next=0;
    while(s[point]!='\0')
    {
        if(s[point]!=' ')
        {
            printf("%c",s[point]);
            point++;
        }
        else
        {
            next=point+1;
            while(s[next]==' ')
                next++;
            printf(" ");
            point=next;
        }
    }
}

Explain part

  1. The above method can only output and cannot obtain the converted value.If you want to implement this function, you can create a new array and assign values ​​one by one, which is very simple to implement;
  2. Traverse the entire array to '\ 0', without traversing all, saving time;
  3. Use next to point to the output position, use point to traverse the value and some operations

Guess you like

Origin www.cnblogs.com/comixH/p/12741026.html