#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 65535
// char *fgets(char *buf, int bufsize, FILE *stream);
void reverseWord(char* b, char* e) {
while(e-b > 0) {
char tmp = *b;
*(b++) = *e;
*(e--) = tmp;
}
}
int main() {
char s[MAX];
fgets(s, MAX, stdin);
// char *fgets(char *buf, int bufsize, FILE *stream);
int slen = strlen(s)-1;
//including the '
', so need minus 1
int splen = 0;
//as it is
for(char* ps = s; ps-s < slen; ps++) {
//get the indexes of blank spaces
if (*ps == ' ') {
splen += 1;
}
}
printf("Space counts: %d
", splen);
int idx[splen];
//indexes of blank space
int* pi = idx;
for(char* ps = s; ps-s < slen; ps++) {
if (*ps == ' ') {
*(pi++) = ps-s;
}
}
char* last = s-1; //上一个空格
char* this = s-1;
for (int* pi = idx; pi-idx < splen; pi++) {
this = s + *pi; //当前空格的位置
if(this > s) { //第一个字符不是空格
char* b = last + 1;
char* e = this - 1;
if(*b < '0' || *b > '9')
reverseWord(b, e);
}
last = this; //做记号,上一个空格在哪
if(pi-idx == splen-1 && this < s+slen-1) {
this = s + slen;
char* b = last + 1;
char* e = this - 1;
if(*b < '0' || *b > '9')
reverseWord(b, e);
}
}
fputs(s, stdout);
return 0;
}