using System;
public class Test
{
public static string ReduceSpaces( string input )
{
char[] a = input.ToCharArray();
int placeComma = 0, placeOther = 0;
bool inQuotes = false;
bool followedComma = true;
foreach( char c in a ) {
inQuotes ^= (c == '\"');
if (c == ' ') {
if (!followedComma)
a[placeOther++] = c;
}
else if (c == ',') {
a[placeComma++] = c;
placeOther = placeComma;
followedComma = true;
}
else {
a[placeOther++] = c;
placeComma = placeOther;
followedComma = false;
}
}
return new String(a, 0, placeComma);
}
public static void Main()
{
string testcase = "1, 4, 2, \"PUBLIC, JOHN Q\" ,ACTIVE , 1332 , TROUBLE COLUMN ";
System.Console.WriteLine("\"" + ReduceSpaces(testcase) + "\"");
}
}
dXNpbmcgU3lzdGVtOwoKcHVibGljIGNsYXNzIFRlc3QKewoJcHVibGljIHN0YXRpYyBzdHJpbmcgUmVkdWNlU3BhY2VzKCBzdHJpbmcgaW5wdXQgKQoJewoJCWNoYXJbXSBhID0gaW5wdXQuVG9DaGFyQXJyYXkoKTsKCQlpbnQgcGxhY2VDb21tYSA9IDAsIHBsYWNlT3RoZXIgPSAwOwoJCWJvb2wgaW5RdW90ZXMgPSBmYWxzZTsKCQlib29sIGZvbGxvd2VkQ29tbWEgPSB0cnVlOwoJCWZvcmVhY2goIGNoYXIgYyBpbiBhICkgewoJCQlpblF1b3RlcyBePSAoYyA9PSAnXCInKTsKCQkJaWYgKGMgPT0gJyAnKSB7CgkJCQlpZiAoIWZvbGxvd2VkQ29tbWEpCgkJCQkJYVtwbGFjZU90aGVyKytdID0gYzsKCQkJfQoJCQllbHNlIGlmIChjID09ICcsJykgewoJCQkJYVtwbGFjZUNvbW1hKytdID0gYzsKCQkJCXBsYWNlT3RoZXIgPSBwbGFjZUNvbW1hOwoJCQkJZm9sbG93ZWRDb21tYSA9IHRydWU7CgkJCX0KCQkJZWxzZSB7CgkJCQlhW3BsYWNlT3RoZXIrK10gPSBjOwoJCQkJcGxhY2VDb21tYSA9IHBsYWNlT3RoZXI7CgkJCQlmb2xsb3dlZENv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