#include <stdio.h>
#include <string.h>
#include <ctype.h>
void removeExtraSpaces(char* str, char* output) {
int i = 0, j = 0;
int n = strlen(str);
int spaceFound = 0;
// Skip leading spaces
while (i < n && str[i] == ' ') {
i++;
}
while (i < n) {
if (!isspace(str[i])) {
output[j++] = str[i];
spaceFound = 0; // Reset the space flag
} else if (!spaceFound) {
output[j++] = ' '; // Add a single space
spaceFound = 1; // Mark space as found
}
i++;
}
// Remove trailing space if any
if (j > 0 && output[j - 1] == ' ') {
j--;
}
output[j] = '\0'; // Null-terminate the output string
}
int main() {
char str[] = " This is a string with multiple spaces ";
char output[128]; // Adjust size based on expected input length
removeExtraSpaces(str, output);
printf("\"%s\"\n", output);
return 0;
}
/*
run:
"This is a string with multiple spaces"
*/