DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #46 - ???

Today's challenge requires you to write a function which removes all question marks from a given string.

For example: hello? would be hello


This challenge comes from aikedaa here on DEV.

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Latest comments (41)

Collapse
 
matrossuch profile image
Mat-R-Such

Python

def remove_(text):  return text.replace('?','')

print(remove_('He?l?lo?'))
Collapse
 
kvharish profile image
K.V.Harish

My solution in js

const removeQuestionMark = (str) => str.replace(/\?/gi, '');
Collapse
 
hectorpascual profile image
Héctor Pascual

As simple as it sounds, in python :

lambda s : re.sub(r'\?','', s)
 
hanachin profile image
Seiei Miyagi • Edited

I see. Also % literal is my favorite string literal :)

s = %
here is string, the delimiter is \\n

puts s
Collapse
 
hanachin profile image
Seiei Miyagi

I love weird Ruby syntax😆

TIPS: ?? is known as character literal notation
docs.ruby-lang.org/en/trunk/syntax...

Collapse
 
choroba profile image
E. Choroba

Perl solution:

my $str = 'a?b?c?';
say $str =~ tr/?//dr;

The tr operator works like the tr shell util. /d means non-replaced characters are deleted, /r returns the value instead of modifying the bound variable.

Collapse
 
ganesh profile image
Ganesh Raja

Python:

print(s.replace('?',''))
Collapse
 
vivek97 profile image
Vivek97 • Edited
System.out.println("??he?llo".replace("?", " "));


`

Collapse
 
idanarye profile image
Idan Arye
// CharacterRemover.java

package org.stringops.questionmarkremover;

public interface CharacterRemover {
    /**
     * TODO
     * @param String source TODO
     * @return String TODO
     */
    public String removeCharacter(String source);
}
// CharacterRemoverFactory.java

package org.stringops.questionmarkremover;

public interface CharacterRemoverFactory {
    /**
     * TODO
     * @return CharacterRemover TODO
     */
    public CharacterRemover createCharacterRemover();
}
// QuestionMarkRemover.java

package org.stringops.questionmarkremover;

import java.io.StringReader;
import java.io.InputStreamReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class QuestionMarkRemover implements CharacterRemover {
    /**
     * TODO
     * @param String source TODO
     * @return String TODO
     */
    public String removeCharacter(String source) {
        StringReader reader = new StringReader(source);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            int dataFromReader = reader.read();
            while (dataFromReader != -1) {
                if (dataFromReader != '?') {
                    outputStream.write(dataFromReader);
                }
                dataFromReader = reader.read();
            }
        } catch (IOException e) {
            // TODO: handle the exception
        }
        byte[] byteArray = outputStream.toByteArray();
        return new String(byteArray);
    }
}
// QuestionMarkRemoverFactory.java

package org.stringops.questionmarkremover;

public class QuestionMarkRemoverFactory implements CharacterRemoverFactory {
    /**
     * TODO
     * @return CharacterRemover TODO
     */
    public CharacterRemover createCharacterRemover() {
        return new QuestionMarkRemover();
    }
}

And to use it:

import org.stringops.questionmarkremover.*;

public class App {
    /**
     * TODO
     * @param String[] args TODO
     * @return void TODO
     */
    public static void main(String[] args) {
        CharacterRemoverFactory characterRemoverFactory = new QuestionMarkRemoverFactory();
        CharacterRemover characterRemover = characterRemoverFactory.createCharacterRemover();

        System.out.println(characterRemover.removeCharacter("hello?"));
    }
}
Collapse
 
gypsydave5 profile image
David Wickes

ENTERPRISE

Collapse
 
brightone profile image
Oleksii Filonenko
private static final OpinionAboutJava opinionAboutJava = OpinionAboutJavaFactory.getOpinionAboutJava("ugh...");
Collapse
 
idanarye profile image
Idan Arye

NOOOO!!!

You need to create an instance of the OpinionAboutJavaFactory! You can just have a static getOpinionAboutJava method! Now your code is not SOLID!

 
room_js profile image
JavaScript Room

I intentionally avoided using sed this time, wanted to implement it with "pure" Bash. Using sed the implementation, of course, becomes much shorter.

Collapse
 
willsmart profile image
willsmart • Edited

Well, since we're doing cmd things...

read s; echo "${s//\?/}"

(bash on mac)

Collapse
 
serbuvlad profile image
Șerbu Vlad Gabriel

x86_64 assembly (System V ABI, GNU assembler), as usual:

bsure.S:

    .global bsure

    .text
bsure:
    xor %eax, %eax
loop:
    mov (%rsi), %cl

    cmp $63, %cl # '?'
    je skip

    mov %cl, (%rdi)
    inc %rdi
    inc %rax
skip:
    inc %rsi

    cmp $0, %cl
    jne loop

    ret

bsure.h:

/* 
 * bsure - remove question marks from a string
 * if dst and src overlap (or match), that's fine
 * returns the number of bytes copied (including the null byte).
 */
size_t bsure(char *dst, char *src);
Collapse
 
gypsydave5 profile image
David Wickes • Edited

sed is overkill. Try tr:

echo "why not use tr?" | tr -d '?'
Collapse
 
stephanie profile image
Stephanie Handsteiner

PHP:

$string = "Some string with question marks??!!";
echo $string = str_replace("?", "", $string);
Collapse
 
chrisachard profile image
Chris Achard

Even though this is a simple one - it's really interesting to see all of the different ways you can do it!

But I agree with Josh - it's built into most languages :)

str.replace("?", "")