카타 8급 altERnaTIng cAsE ⇔ ALTerNAtiNG CaSe

1 C[ | ]

char *to_alternating_case(const char *s) {
  int len = strlen(s);
  char* res = strdup(s);
  for(int i=0; i<len;i++) {
    if( isalpha(res[i]) ) res[i] ^= 0x20;
  }
  return res;
}
char *to_alternating_case(const char *s) {
  int len = strlen(s);
  char* res = strdup(s);
  for(int i=0; i<len; i++) {
    if(res[i]<='z' && res[i]>='a') res[i] -= 32;
    else if(res[i] <='Z' && res[i] >='A') res[i] += 32;
  }
  return res;
}
char *to_alternating_case(const char *s) {
  char *res = (char *)malloc(strlen(s) + 1);
  char *p = res;
  while (*s) {
    if (isalpha(*s)) *p++ = *s++ ^ 0x20;
    else *p++ = *s++;
  }
  *p = 0;
  return res;
}

2 C++[ | ]

#include <iostream>
string to_alternating_case(const string& str)
{
  string ret = str;
  for (char &c : ret) c = isupper(c) ? tolower(c) : toupper(c);
  return ret;
}
#include <iostream>
string to_alternating_case(const string& str)
{
  string result = str;
  for_each(result.begin(), result.end(), [&] (char &c) { c = isupper(c)? tolower(c) : toupper(c);});
  return result;
}
#include <iostream>
string to_alternating_case(const string& str)
{
  string res = str;
  for(int i=0; i<str.length(); i++) {
    if(res[i]<='z' && res[i]>='a') res[i] -= 32;
    else if(res[i] <='Z' && res[i] >='A') res[i] += 32;
  }
  return res;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}