C, 215 바이트 , 196 바이트
@tucuxi 덕분에 19 바이트가 절약되었습니다!
골프 :
char i[99],o[999],b[99],z[99];t,x,n,c;main(){gets(i);sscanf(i,"%s %[^[]s",b,z);while(sscanf(i+t,"%*[^0-9]%d%n",&x,&n)==1)sprintf(o,"%s[%d] = %d;\n",z,c++,x),t+=n;printf("%s %s[%d];\n%s",b,z,c,o);}
언 골프 드 :
/*
* Global strings:
* i: input string
* o: output string
* b: input array type
* z: input array name
*/
char i[ 99 ], o[ 999 ], b[ 99 ], z[ 99 ];
/* Global ints initialized to zeros */
t, x, n, c;
main()
{
/* Grab input string from stdin, store into i */
gets( i );
/* Grab the <type> <array_name> and store into b and z */
sscanf( i, "%s %[^[]s", b, z );
/* Grab only the int values and concatenate to output string */
while( sscanf( i + t, "%*[^0-9]%d%n", &x, &n ) == 1 )
{
/* Format the string and store into a */
sprintf( o, "%s[%d] = %d;\n", z, c++, x );
/* Get the current location of the pointer */
t += n;
}
/* Print the <type> <array_name>[<size>]; and output string */
printf( "%s %s[%d];\n%s", b, z, c, o );
}
링크:
http://ideone.com/h81XbI
설명:
를 얻으려면 <type> <array_name>
의 sscanf()
형식 문자열이 있습니다 :
%s A string delimited by a space
%[^[] The character set that contains anything but a `[` symbol
s A string of that character set
string에서 int 값을 추출하려면 int foo[] = {4, 8, 15, 16, 23, 42};
본질적 으로이 함수를 사용하여 문자열을 토큰 화하십시오.
while( sscanf( i + t, "%*[^0-9]%d%n", &x, &n ) == 1 )
어디:
i
입력 문자열입니다 (a char*
)
t
포인터 위치 오프셋 i
x
int
문자열에서 실제 파싱됩니다.
n
찾은 숫자를 포함하여 소비 된 총 문자입니다.
sscanf()
형식 문자열이 의미 :
%* Ignore the following, which is..
[^0-9] ..anything that isn't a digit
%d Read and store the digit found
%n Store the number of characters consumed
입력 문자열을 char 배열로 시각화하는 경우 :
int foo[] = {4, 8, 15, 16, 23, 42};
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
00000000001111111111222222222233333
01234567890123456789012345678901234
가와 int
4
, 인덱스 (13)에 위치되는 8
등 지수 16이 무엇 같은 루프 모양의 각 실행의 결과입니다 :
Run 1) String: "int foo[] = {4, 8, 15, 16, 23, 42};"
Starting string pointer: str[ 0 ]
Num chars consumed until after found digit: 14
Digit that was found: 4
Ending string pointer: str[ 14 ]
Run 2) String: ", 8, 15, 16, 23, 42};"
Starting string pointer: str[ 14 ]
Num chars consumed until after found digit: 3
Digit that was found: 8
Ending string pointer: str[ 17 ]
Run 3) String: ", 15, 16, 23, 42};"
Starting string pointer: str[ 17 ]
Num chars consumed until after found digit: 4
Digit that was found: 15
Ending string pointer: str[ 21 ]
Run 4) String: ", 16, 23, 42};"
Starting string pointer: str[ 21 ]
Num chars consumed until after found digit: 4
Digit that was found: 16
Ending string pointer: str[ 25 ]
Run 5) String: ", 23, 42};"
Starting string pointer: str[ 25 ]
Num chars consumed until after found digit: 4
Digit that was found: 23
Ending string pointer: str[ 29 ]
Run 6) String: ", 42};"
Starting string pointer: str[ 29 ]
Num chars consumed until after found digit: 4
Digit that was found: 42
Ending string pointer: str[ 33 ]
foo[0]=1;
받아 들일 수 있습니까?