말 그대로 CR과 LF를 가져 가라. 두 번의 캐리지 리턴은 단일 라인 피드로

오래된 메모장을 축하 하기 위해 캐리지 리턴과 줄 바꿈을 오늘날 사용 된 것이 아니라 원래의 의미로 취급합니다.

인쇄 가능한 ASCII와 줄 바꿈 (␊; LF; esc \n; hex 0A; dec 10)과 캐리지 리턴 (␍; CR; esc \r; hex 0D; dec 13) 으로 구성된 문자열이 있으면 온라인 으로 시도 하여 인쇄 가능한 문자를 표시하십시오. 이 두 제어 문자를 문자 그대로 사용하는 프린터에 인쇄 된 경우 배치 됩니다.

  1. 줄 바꿈시 한 줄 더 아래로 계속 인쇄
  2. 캐리지 리턴시 왼쪽 가장자리에서 계속 인쇄
  3. 여러 개의 연속 캐리지 리턴은 단일 캐리지 리턴처럼 작동합니다

오버 스트라이킹에 문제가있는 최신 장치로 인해 입력 시작 부분을 제외하고 하나 이상의 선행 및 / 또는 후속 라인 피드 없이는 하나 이상의 캐리지 리턴이 실행되지 않습니다. 그러나 두 번의 캐리지 리턴은 단일 라인 피드로 분리 될 수 있습니다.

최소한 입력에 제공된 공백의 양이 유지되는 한, 줄의 오른쪽과 전체 텍스트 아래에 추가 공백이 허용됩니다.

예 ( 라인 피드 및 캐리지 리턴 사용 \n및 사용 \r)

Lorem ipsum dolor sit amet,

Lorem ipsum dolor sit amet,

consectetur adipiscing\nelit, sed

consectetur adipiscing
                      elit, sed

do eiusmod\r\ntempor incididunt\n\n ut labore

do eiusmod
tempor incididunt

                  ut labore

et dolore\n\rmagna \r\r\naliqua. Ut (후행 공백에 유의하십시오)

et dolore
magna
aliqua. Ut

\nenim ad minim veniam,\n\r quis nostrud

베니 암
     퀴스 노스 스 트루

\rexercitation\r\n\rullamco laboris\n\r\nnisi ut aliquip ex\n\n\rea commodo consequat.\n\n

운동
Ullamco Laboris

nisi ut aliquip 전

commodo 결과.




답변

, 10 바이트

UTFθ«ι≡ι⸿↑

온라인으로 사용해보십시오! 링크는 자세한 버전의 코드입니다. 설명:

UT

오른쪽 여백을 비활성화합니다.

Fθ«

입력을 반복합니다.

ι

현재 문자를 인쇄하십시오. 이것은 자동으로 처리 되지만 (이 상황에서 \n숯으로 처리됩니다) 숯은로 \v변환 \r됩니다 \r\n.

≡ι⸿

… 확인 \r

… 그렇다면 줄을 다시 위로 이동하십시오.


답변

루비 , 24 17 바이트

->s{s.tr $/,"\v"}

온라인으로 사용해보십시오!

TIO에서는 작동하지 않지만 Linux 콘솔에서는 작동합니다.


답변

자바 10 211 207 206 바이트

s->{var a=s.replace("\r\n","\n\r").split("(?<=\n)");int i=0,p=0,j;for(var x:a){for(j=x.charAt(0)<14?0:p;j-->0;x=" "+x);j=(a[i++]=x.replace("\r","")).length()-1;p=x.matches("\\s+")?p:j;}return"".join("",a);}

온라인으로 사용해보십시오.

설명:

s->{                      // Method with String as both parameter and return-type
  var a=s.replace("\r\n","\n\r")
                          //  Replace all "\r\n" with "\n\r"
        .split("(?<=\n)");//  Create String-array split by "\n",
                          //  without removing the trailing "\n" delimiter
  int i=0,                //  Index integer
      p=0,                //  Previous line-length, starting at 0
      j;                  //  Temp integer
  for(var x:a){           //  Loop over the String-array
    for(j=x.charAt(0)<14?0//   If the current line starts with either '\r' or '\n':
        0                 //    Prepend no spaces
       :                  //   Else:
        p;j-->0;x=" "+x); //    Prepand `p` amount of spaces for the current item
    j=(a[i++]=x.replace("\r",""))
                          //   Remove all "\r" from the current line
       .length()-1;       //   Set `j` to the current line-length (minus the trailing '\n')
    p=x.matches("\\s+")?  //   If the current line only contains '\r', '\n' and/or spaces:
       p                  //    Leave `p` unchanged
      :                   //   Else:
       j;}                //    Change `p` to this line-length minus 1
  return"".join("",a);}   //  Return the String-array joined together

도전하기 전에 오래된 대답이 변경되었습니다 (151) 148 바이트를 :

s->{String a[]=s.replace("\r\n","\n\r").split("(?<=\n)"),r="";int p=0,i;for(var x:a){for(i=p;i-->0;r+=" ");i=x.length()-1;p=i<1?p:i;r+=x;}return r;}

설명:

s->{                            // Method with String as both parameter and return-type
  String a[]=s.replace("\r\n","\n\r")
                                //  Replace all "\r\n" with "\n\r"
              .split("(?<=\n)"),//  Create String-array split by "\n",
                                //  without removing the trailing "\n" delimiter
         r="";                  //  Result-String, starting empty
  int p=0,                      //  Previous line-length, starting at 0
      i;                        //  Index (and temp) integer
  for(var x:a){                 //  Loop over the String-array
    for(i=p;i-->0;r+=" ");      //   Append `p` amount of spaces to the result
    i=x.length()-1;p=i<1?p:j;   //   If the current line is not empty:
                                //    Replace `p` with the length of this current line
    r+=x;}                      //   Append the current item
  return r;}                    //  Return the result-String

TIO에서는 작동하지 않으며 Windows 명령 프롬프트에서는 작동합니다.

여기에 이미지 설명을 입력하십시오


답변

자바 스크립트 (Node.js) , 85 바이트

s=>s[R='replace'](/(.*)([\n\r]+)/g,(_,t,b)=>t+b[R](/\r/g,_=>s='',s=t[R](/./g,' '))+s)

온라인으로 사용해보십시오!


답변

파이썬 (2) , 150 (128) 122 104 103 바이트

def f(s):
 i=n=0;l=''
 for c in s:l,n,i=[l,l+c,l+' '*i*n+c,n,1,0,0,i,i+1]['\r\n'.find(c)%3::3]
 print l

온라인으로 사용해보십시오!


저장 됨 :

  • Lynn 덕분에 -1 바이트

답변

C (gcc) , 100 94 바이트

b,c,d;f(char*s){for(b=13;b;b=*s++)b==13?c=d=0:b-10?d=!printf("%*c",++d,b),++c:putchar(b,d=c);}

ASCII 코딩 ( '\r'==13, '\n'==10)을 가정합니다 . 다른 시스템에 맞게 조정하십시오.

온라인으로 사용해보십시오! (자바 스크립트 필요)

읽을 수있는 버전

int c = 0;
int d = 0;

f(char*s)
{
    for (;*s;++s) {
        switch (*s) {
        case'\r':
            c = d = 0;
            break;
        case'\n':
            d = c;
            putchar(*s);
            break;
        default:
            printf("%*s%c", d, "", *s);
            d = 0;
            ++c;
        }
    }
}

c현재 열 위치입니다. d인쇄 가능한 문자 앞에 삽입해야하는 공백 수입니다. 함수 시작시 둘 다 0으로 가정됩니다.

테스트 프로그램

int main(int argc, char **argv)
{
    char s[1024];
    if (argc <= 1)
        while (fgets(s, sizeof s, stdin))
               f(s);
    else
        for (int i = 1;  i < argc;  ++i)
            f(argv[i]);
}


답변

파이썬 3 , 101 94 바이트

TFeld의 답변을 기반으로 합니다.

def f(s):
 i=n=0
 for c in s:k='\r\n'.find(c);a=k&1;print(end=-k*' '*i*n+c*a);n=k>0;i=i*a-k//2

온라인으로 사용해보십시오!


언 골프

def f(s):
  i=0  # position of the cursor
  n=0  # was the last character LF?
  for c in s:        # iterate over the input
    k='\r\n'.find(c) # k := 0 for CR, 1 for LF and -1 for every other character
    a=k&1            # as (-1)&1 == (1)&1 == 1, this is a := abs(k)
    print(end=-k*' '*i*n+c*a) # If c is a normal character (k == -1) and the last character was LF, 
                              # print leading spaces. If c is not CR, print it
    n=k>0            # n := True if c is LF, False otherwise
    i=i*a-k//2       # If c is either a newline or a printable character (a == 1),
                     # keep the cursor's position and increment it for a printable character ((-1)//2 == -1)