将字符串中多余的空格去掉

#include<stdio.h>

#include<stdlib.h>



int main(void)

{

    char str[100];

    char des[100];



    printf("input a string:");

    gets(str);



    int i = 0;

    int j = 0;

    int blank_count = 0;

    char c = str[0];

    for(i=1; c!='\0'; i++)

    {

        if(c != ' ')

        {    

            if(blank_count > 0)

            {

                des[j++] = ' ';

                blank_count = 0;

            }

            des[j++] = c;

        }

        else

        {

            blank_count++;

        }



        c = str[i];

    }



    printf("str:%s\n", str);

    printf("des:%s\n", des);



    return EXIT_SUCCESS;

}

 

你可能感兴趣的:(字符串)