27
\$\begingroup\$

In the C standard library, header names end with a .h suffix:

stdio.h

In C++, those header names are available in an alternative form, with a c prefix instead:

cstdio

Write a function that converts the first form into the second. You can do the conversion in-place, or leave the original string intact and return a new string. Whatever feelds natural in your language of choice.

The code must be compiled/interpreted without errors. Compiler warnings are acceptable.

Here is your baseline C solution. It has 70 characters and generates a warning about strlen:

void f(char*h){int i=strlen(h);h[--i]=0;while(--i)h[i]=h[i-1];*h='c';}

The shortest solution (measured in number of characters) wins.

Update: If your language of choice does not support functions, whole programs are also acceptable.

Update: As suggested by FUZxxl, here is a complete list of the header files in the C standard library:

assert.h
ctype.h
errno.h
float.h
limits.h
locale.h
math.h
setjmp.h
signal.h
stdarg.h
stddef.h
stdio.h
stdlib.h
string.h
time.h

Specifically, there are no header names with multiple dots in them.

\$\endgroup\$

51 Answers 51

1
\$\begingroup\$

MS QuickBasic or QB64 ~ 33 bytes

A$=left$(A$,len(A$)-2)
A$="c"+A$

A$ is the name of the file

\$\endgroup\$
1
\$\begingroup\$

Ruby: 16 characters

->x{?c+x[0..-3]}

Or, more correct solution as suggested by @mbuettner

->x{?c+x[/\w+/]}

You can call it like this:

->x{?c+x[/\w+/]}.call 'stdio.h'
# cstdio
\$\endgroup\$
  • \$\begingroup\$ XD damnn.. XD XD \$\endgroup\$ – Kokizzu Feb 14 '15 at 1:08
1
\$\begingroup\$

C++ 14, 48

[](string s){return "c"+s.substr(0,s.size()-2);}
\$\endgroup\$
1
\$\begingroup\$

Go, 47,46

func h(s string)string{return"c"+s[:len(s)-2]}

I know I'm not winning anything with this, but I thought I might as well go for it. No pun intended

\$\endgroup\$
  • \$\begingroup\$ I think you can drop the space between return and "c". \$\endgroup\$ – FUZxxl Feb 14 '15 at 14:55
  • \$\begingroup\$ @FUZxxl I didn't know I could do that, thanks for pointing that out! \$\endgroup\$ – Elenian Feb 14 '15 at 16:17
1
\$\begingroup\$

><>, 22 21 bytes

Since full programs are now allowed...

i:0(?v
~}-5~/r
o;!?l<

Explanation

i:0(?v      Keep reading input until EOF
~}-5~/r     Pop the -1 from EOF, turn 'h' -> 'c' with 5- and shift to back, pop '.', reverse stack
o;!?l<      Keep outputting until the stack is empty
\$\endgroup\$
1
\$\begingroup\$

K, 7 bytes

"c",-2_

Pretty simple. Just drops 2 chars off the end and prepends a c.

Also, despite how it looks, this is actually a function:

f:"c",-2_
f"stdio.h"
\$\endgroup\$
1
\$\begingroup\$

Awk: 10 characters

$0="c"$1

Sample run:

bash-4.3$ awk -F. '$0="c"$1' <<< 'stdio.h'
cstdio
\$\endgroup\$
1
\$\begingroup\$

Excel formula, 36 bytes

If the header is put in cell A1,

=CONCATENATE("c",LEFT(A1,LEN(A1)-2))

Explanation

=CONCATENATE("c",LEFT(A1,LEN(A1)-2))
=CONCATENATE(   ,                  )  Adds two strings
             "c",                     The letter 'c'
                 LEFT(            )   Returns the specified # of chars from the beginning
                      A1,             Cell A1
                         LEN(A1)-2    The length of A1, minus 2 from the '.h'
\$\endgroup\$
1
\$\begingroup\$

Whitespace, 63 bytes

[S S S T    T   S S S T T   N
_Push_99_c][T   N
S S _Print_as_character][N
S S N
_Create_Label_LOOP][S S S N
S_Push_0][S N
S _Duplicate][T N
T   S _Read_STDIN_as_character][T   T   T   _Retrieve][S N
S _Duplicate][S S S T   S T T   T   S N
_Push_46_.][T   S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_EXIT][T N
S S _Print_as_character][N
S N
N
_Jump_to_Label_LOOP]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Print "c"
Start LOOP
  Character i = STDIN as character
  if(i == '.')
    EXIT program (with error)
  Print i
  Go to next iteration of LOOP

Program stops with an error: Label_EXIT not found.

\$\endgroup\$
1
\$\begingroup\$

Japt, 7 6 bytes

ic ¯-2
ic     # Given the default input, prepend a 'c' at the start
   ¯-2 # and trim off the last two letters.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Save a byte by omitting the '. \$\endgroup\$ – Shaggy May 14 '18 at 13:20
  • \$\begingroup\$ @Shaggy Thanks, I often forget Japt handles inputs as defaulting to chars in these cases. \$\endgroup\$ – Nit May 14 '18 at 13:37
0
\$\begingroup\$

Julia: 19 characters

f(s)="c"*s[1:end-2]

Sample run:

julia> f(s)="c"*s[1:end-2]
f (generic function with 1 method)

julia> print(f("stdio.h"))
cstdio
\$\endgroup\$
0
\$\begingroup\$

Perl (program) 15

s/(.*)\.h/c\1/

It converts filenames supplied on std input, and needs to be run with the p flag, so that's one more character I believe. Eg

echo stdio.h | perl -p header.pl

output:

cstdio

Note to sticklers: I just spotted that this may be invalid because challenge implies that programs are only acceptable if the language doesn't support functions. However, I'm not going to throw it all away at this stage! Ah well.

Update: Augh! And I hadn't spotted page 2 of the answers either. Please refer to @malkaroee's effectively identical answer posted 4 months ago. ... Hang on, who resurrected this thread?

\$\endgroup\$
0
\$\begingroup\$

Lua, 39 bytes

function f(n)return "c"..n:sub(1,-3)end
\$\endgroup\$
0
\$\begingroup\$

C# 6, 52 bytes

class P{string C(string f)=>"c"+f.Replace(".h","");}

C# <6, 58 bytes

class P{string C(string f){return"c"+f.Replace(".h","");}}
\$\endgroup\$
0
\$\begingroup\$

TI-89 Basic, 56 bytes

:Def a(a)=Func
:Return "c"+sub(a,1,length(a)-2
:End Func
\$\endgroup\$
0
\$\begingroup\$

MATLAB, 19

@(c)[99,c(1:end-2)]

Fairly straight forward. Prepends 'c' to the input string less the last two letters. MATLAB automatically converts the 99 (ASCII value) to 'c' when appending to a character string.

\$\endgroup\$
0
\$\begingroup\$

Tcl, 36 bytes

proc H h {set s c[regsub ..$ $h ""]}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

V, 5 bytes

éc$xx

Try it online!

Hexdump:

00000000: e963 2478 78                             .c$xx

This is a full program that takes input from STDIN.

Explanation

éc     insert the character 'c'
$      go to the end of the line
xx     delete two characters

This is ic<esc>$xx in pure Vim.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 5 bytes

¨¨'cì

Try it online.
Header ε is used to for-each over the input-list to verify all test cases instead of just one.
Footer is to close this for-each, and print the results with new-line delimiter.

Explanation:

¨¨       # Remove the last two characters of the input
    ì    # Prepend:
  'c     #  "c"
\$\endgroup\$
  • \$\begingroup\$ Technically not competing since the whole language was made after the question was asked. \$\endgroup\$ – Nit May 7 '18 at 9:54
  • 1
    \$\begingroup\$ @Nit Not anymore since the Summer of 2017. Relevant meta post. \$\endgroup\$ – Kevin Cruijssen May 7 '18 at 9:57
  • 1
    \$\begingroup\$ Ah, didn't know that, thanks for the heads up. \$\endgroup\$ – Nit May 7 '18 at 10:02
0
\$\begingroup\$

Stackers (27 bytes)

«h ®h 👁h cc ( ~ ~ 🖹

Ungolfed Version

«h "define a table called h
®h "read STDIN to h
👁h "read h to the stack
cc  "add the value for 'c' to the stack
(   "put c on the bottom of the stack
~ ~ "destroy the top two values on the stack
🖹   "print stack bottom two top

Stackers is a programming language I created myself that is meant to be based on stacks, with variables and tables thrown in

\$\endgroup\$
0
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 41 bytes

	INPUT ARB . O RPOS(2)
	OUTPUT ='c' O
END

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.