UVA Problem 11965 – Extra Spaces Solution

UVA Problem 11965 – Extra Spaces Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

This problem is very straightforward. We just need to skip outputting consecutive spaces. Instead of multiple spaces only print one space.

The only problem for this problem may be outputting a blank line between test cases. This can be handled in many ways. I have shown one method below. I have commented the code below, have a look.

Important:  Be sure to add or print a new line after each output unless otherwise specified. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Critical Input:

4
3
Sample test one:
  there was     2 spaces and 
here are also  2  spaces
2
Sample test two:
     there was 4 spaces
3
Sample         test one:
  there was 2 spaces and 
here are also  2    spaces
2
Sample test two:
     there was 4 spaces

 


Output:

Case 1:
Sample test one:
 there was 2 spaces and 
here are also 2 spaces

Case 2:
Sample test two:
 there was 4 spaces

Case 3:
Sample test one:
 there was 2 spaces and 
here are also 2 spaces

Case 4:
Sample test two:
 there was 4 spaces

 

Code:

/**
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 */

#include<cstdio>

static char buffer[1024];

int main(){
	register unsigned int n, m, c = 0;
	scanf("%u", &n);

	while (n--){

        /*
         * Use getchar() before reading a string or character to discard the newline character,
         * Otherwise newline character will be taken taken by the string or character input function
         */
        scanf("%u", &m); getchar();

        /*
         * Only print newline between test cases
         */
        if (c) printf("\n");

        ++c;
        printf("Case %u:\n", c);

        while (m--){
            gets(buffer);

            register unsigned i = 0, spaces = 1;
            unsigned val;

            for (; val = buffer[i]; ++i){

                /*
                 * Cutting Comparison
                 * @example use this code {{ val = val == ' ' }}
                 * replace {{ val == ' ' }} statements with just {{ val }}
                 */

                /*
                 * If a space found after non space character then print a space
                 */
                if (val == ' ' && spaces){
                    printf(" ");
                    spaces = 0;
                    continue;
                }

                /*
                 * If consecutive space are found skip them
                 */
                if (val == ' ') continue;

                /*
                 * Print non space characters
                 */
                printf("%c", buffer[i]);
                spaces = 1;
            }
            printf("\n");
        }
	}
	return 0;
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s