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.