毫无状态

class Solution {
public:
    string trans(string s, int n) {
      if (n == 0) {
        return s;
      }
      
      for (int i = 0; i < n; ++i) {
        if (s[i] >= 'A' && s[i] <= 'Z') {
          s[i] = std::tolower(s[i]);
        } else if (s[i] >= 'a' && s[i] <= 'z') {
          s[i] = std::toupper(s[i]);
        } 
      }
      
      std::reverse(s.begin(), s.end());
      
      for (int i = 0; i < n; ++i) {
        int j = i;
        while (s[j] != ' ' && j < n) {
          ++j;
        }
        std::reverse(s.begin() + i, s.begin() + j);
        
        i = j;
      }
      
      return s;
    }
};
/**
 * 
 * @param s string字符串 
 * @param n int整型 
 * @return string字符串
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */
char* trans(char* s, int n ) {
  char *str = (char *)malloc(strlen(s) + 1);
  memcpy(str, s, strlen(s) + 1);
  
  for (int i = 0; i < n; ++i) {
    if (str[i] >= 'A' && str[i] <= 'Z') {
      str[i] = tolower(str[i]);
    } else if (str[i] >= 'a' && str[i] <= 'z') {
      str[i] = toupper(str[i]);
    }
  }
  
  for (int i = 0, j = n - 1; i < j; ++i, --j) {
    char tmp = str[i];
    str[i] = str[j];
    str[j] = tmp;
  }
  
  for (int i = 0; i < n; ++i) {
    int j = i;
    while (str[j] != ' ' && j < n) {
      ++j;
    }
    
    int front = i, back = j - 1;
    
    while (front < back) {
      char tmp = str[front];
      str[front] = str[back];
      str[back] = tmp;
      ++front;
      --back;
    }
    
    i = j;
  }
  
  return str;
}