#include <iostream>
using namespace std;

const int MAX_SIZE = 256;

void inputString(char[], int);
void trim(char[]);


int main() {
	
	char string[MAX_SIZE];
	inputString(string, MAX_SIZE);

	trim(string);
		
	cout << "Result: >" << string << "<" << endl;
	
	return 0;
}

void inputString(char string[], int limit) {
	cout << "Input a string (max " << limit << "): ";
	cin.getline(string, limit);
}

void trim(char string[]) {

	int readIndex = 0;
	int writeIndex = 0;

	while(string[readIndex] == ' ') {
		++readIndex;
	}
	
	while(string[readIndex] != '\0') {
		if(string[readIndex] == ' ' && string[readIndex - 1] == ' ') {
			++readIndex;
		} else {
			string[writeIndex] = string[readIndex];
			++readIndex;
			++writeIndex;
		}	
	}

	string[writeIndex--] = '\0';
	if(string[writeIndex] == ' ') {
		string[writeIndex] = '\0';
	}
}
