Remove extra white space from inside a C string?

前端 未结 9 2295
心在旅途
心在旅途 2021-01-06 04:05

I have read a few lines of text into an array of C-strings. The lines have an arbitrary number of tab or space-delimited columns, and I am trying to figure out how to remove

9条回答
  •  既然无缘
    2021-01-06 04:36

    The following code simply takes input character wise, then check for each character if there is space more than once it skips it else it prints the character. Same logic you can use for tab also. Hope it helps in solving your problem. If there is any problem with this code please let me know.

        int c, count = 0;
        printf ("Please enter your sentence\n");
        while ( ( c = getchar() ) != EOF )  {
            if ( c != ' ' )  {
                putchar ( c );
                count = 0;
            }
            else  {
                count ++;
                if ( count > 1 )
                    ;    /* Empty if body */
                else
                    putchar ( c );
             }
         }
    }
    

提交回复
热议问题