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\$
0

55 Answers 55

1
2
1
\$\begingroup\$

R, 33

function(x)sub("(.+)..","c\\1",x)

This function uses regular expressions.

Example usage:

> (function(x)sub("(.+)..","c\\1",x))("stdio.h")
[1] "cstdio"
\$\endgroup\$
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\$
1
  • \$\begingroup\$ XD damnn.. XD XD \$\endgroup\$
    – Kokizzu
    Feb 14, 2015 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\$
2
  • \$\begingroup\$ I think you can drop the space between return and "c". \$\endgroup\$
    – FUZxxl
    Feb 14, 2015 at 14:55
  • \$\begingroup\$ @FUZxxl I didn't know I could do that, thanks for pointing that out! \$\endgroup\$
    – Elenian
    Feb 14, 2015 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\$

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\$
1
\$\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\$
3
  • \$\begingroup\$ Technically not competing since the whole language was made after the question was asked. \$\endgroup\$
    – Etheryte
    May 7, 2018 at 9:54
  • 1
    \$\begingroup\$ @Nit Not anymore since the Summer of 2017. Relevant meta post. \$\endgroup\$ May 7, 2018 at 9:57
  • 1
    \$\begingroup\$ Ah, didn't know that, thanks for the heads up. \$\endgroup\$
    – Etheryte
    May 7, 2018 at 10:02
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\$
2
  • \$\begingroup\$ Save a byte by omitting the '. \$\endgroup\$
    – Shaggy
    May 14, 2018 at 13:20
  • \$\begingroup\$ @Shaggy Thanks, I often forget Japt handles inputs as defaulting to chars in these cases. \$\endgroup\$
    – Etheryte
    May 14, 2018 at 13:37
1
\$\begingroup\$

Excel formula, 36 23 bytes

If the input is put in cell A1,

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

Explanation

="c"&LEFT(A1,LEN(A1)-2)
="c"&                     Return the letter 'c' concatenated with...
     LEFT(            )   the first...
             LEN(A1)-2    (the length of input, minus 2 from the '.h') characters...
          A1,             of the input.
\$\endgroup\$
1
\$\begingroup\$

Acc!!, 43 bytes

Write 99
N
Count i while _/96 {
Write _
N
}

Try it online!

Algorithm

  • Write c
  • Read a character into the accumulator
  • Loop while that character is a lowercase letter:
    • Write the character
    • Read another character
\$\endgroup\$
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\$

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\$
0
\$\begingroup\$

Thunno 2 j, 4 bytes

2ẓ'c

Attempt This Online!

Explanation

2ẓ'c  '# Implicit input
2ẓ     # Remove last two characters
  'c  '# Push the character "c"
       # Join the stack
       # Implicit output
\$\endgroup\$
0
\$\begingroup\$

Raku, 13 bytes

{'c'~S/..$//}

Try it online!

S/..$// returns the result of chopping off the last two characters of the string, and 'c' ~ prepends a "c" to the front.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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