카타 8급 Remove String Spaces

Jmnote (토론 | 기여)님의 2019년 3월 17일 (일) 14:53 판

1 C

#include <stdlib.h>
char *no_space(char *strin) {
  int len = strlen(strin);
  char *res = malloc(len);
  int offset = 0;
  for(int i=0; i<len; i++) {
    while(strin[i+offset]==' ') offset++;
    res[i] = strin[i+offset];
  }
  return res;
}
#include <stdlib.h>
char *no_space(char *strin) {
  char *res = malloc(strlen(strin)+1);
  char *p = res;  
  while(*strin) {
    if(*strin != ' ') *p++ = *strin;
    strin++;
  }
  *p='\0';
  return res;
}

2 C++

std::string no_space(std::string x)
{
  x.erase(std::remove(x.begin(), x.end(), ' '), x.end());
  return x;
}
std::string no_space(std::string x)
{    
    std::string temp="";
    for(int i = 0;i<x.size();i++){
      if(x[i]!=' ')
        temp+=x[i];
    }
    return temp;
}

3 PHP

function no_space(string $s): string {
  return str_replace(' ','',$s);
}
function no_space(string $s): string {
  return preg_replace("/\s/", "", $s);
}

4 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}