Backslash outside quotes

I’m stuck on Stage #(Backslash outside quotes).

I’ve tried … (mention what you’ve tried so far).

i have tried to add the \ code to skip it and append the next character

Here are my logs:

remote: [your-program] $ echo script\ \ \ \ \ \ shell
remote: [your-program] script      shell
remote: [tester::#YT5] ✓ Received expected response
remote: [your-program] $ echo \'\"shell hello\"\'
remote: [your-program] '"shell hello"'
remote: [tester::#YT5] ✓ Received expected response
remote: [your-program] $ echo hello\nexample
remote: [your-program] hellonexample
remote: [tester::#YT5] ✓ Received expected response
remote: [your-program] $ cat "/tmp/baz/f\n70" "/tmp/baz/f\48" "/tmp/baz/f'\'94"
remote: [your-program] $
remote: [tester::#YT5] Output does not match expected value.
remote: [tester::#YT5] Expected: "grape pineapple.banana orange.strawberry orange."
remote: [tester::#YT5] Received: "$ "
remote: [tester::#YT5] Assertion failed.
remote: [tester::#YT5] Test failed

And here’s a snippet of my code:

package main.java;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Quotations {
    public static List<String> quote(String input) throws IOException {
        List<String> tokens = new ArrayList<>();
        StringBuilder currentToken = new StringBuilder();
        boolean isSingleQuoted = false;
        boolean isDoubleQuoted = false;
        String[] insideDoubleQuotes = {"\\","$","newline","`","\"","'"};

        // Loop for the Single Quotations
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if(c == '\\'){
                i++;
                if(i < input.length()){
                    currentToken.append(input.charAt(i));
                   
                }
                continue;
            }
            if (c == '\'') {
                if(!isDoubleQuoted){
                    isSingleQuoted = !isSingleQuoted;
                }
                else{
                    currentToken.append(c);
                }
                continue;
            }
            if (c == '"'){
                if(!isSingleQuoted){
                    isDoubleQuoted = !isDoubleQuoted;
                }
                else{
                    currentToken.append(c);
                }
                continue;
            }
            for(String word : insideDoubleQuotes){
                if(c == word.charAt(0) && isDoubleQuoted){
                    continue;
                }
            }
            if (Character.isWhitespace(c) && !isSingleQuoted && !isDoubleQuoted) {
                if (currentToken.length() > 0) {
                    tokens.add(currentToken.toString());
                    currentToken.setLength(0);
                }
            } else {
                currentToken.append(c);
            }
        }

        if (currentToken.length() > 0) {
            tokens.add(currentToken.toString());
        }
        if (isSingleQuoted) {
            System.err.println("unmatched single quote");
        }
        if(isDoubleQuoted){
            System.err.println("unmatched double quote");
        }
        return tokens;
    }
}

Hi @AkshayGadepally, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Hi @andy1li

Link to the the implementation

Any update on this ?

@AkshayGadepally, I tried running your code against the previous stages, but it’s actually no longer passing the previous stage #MG5 (The type builtin: executable files).

Suggestions:

  1. Use our CLI to test against previous stages by running:
codecrafters test --previous
  1. Focus on fixing the early stages first, as later stages depend on them.

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!