-1

In the code I am converting every character to '(' if the appears only once in the whole string, or to ')' if it appears more than once. I pass almost all tests, except the test with input "$$\". It gives "missing terminating character" error. I see that problem is the '\' char. and if I add a second '\' it is good, but is there a quick way to fix it, or I should somehow add 1 to the size of the pointer and then add the second '\'?

P.S. The input is fixed. I cannot change it.

char text[] = "$$\";
char *res = malloc(strlen(text));
int counter = 0;
for(int i = 0; i < strlen(text); i++) {
    for(int j = 0; j < strlen(text); j++) {
        if( tolower(text[i]) == tolower(text[j]) )  {
            counter++;
        }
    }
    if(counter == 1) {
        res[i] = '(';
    } else {
        res[i] = ')';
    }
    printf("%c", res[i]);
    counter = 0;
}
return 0;
zaro
  • 75
  • 5
  • No, just use "\\" when you mean a single backslash in your string. – Ctx Feb 19 '21 at 12:57
  • 1
    Does this answer your question? [using \ in a string as literal instead of an escape](https://stackoverflow.com/questions/12103445/using-in-a-string-as-literal-instead-of-an-escape) – jo3rn Feb 19 '21 at 12:59
  • So I should make a for loop and replace the '\' in the input with '\\'? – zaro Feb 19 '21 at 13:00
  • It will depend on how you are generating the input and which language you are using the input. Using replace function should be better than manual replacement via for loop if it is available in your language. – MikeCAT Feb 19 '21 at 13:00
  • zaro, don't think so complicated. If you want a String a\b then you write "a\\b" in c. That's all. 3 Character string – Ctx Feb 19 '21 at 13:01
  • A quick way to fix is adding \ character. – MikeCAT Feb 19 '21 at 13:02
  • But the input is fixed, I mean I can't fix "$$\" to "$$\\" – zaro Feb 19 '21 at 13:02
  • C doesn't support that, so stop using C then. Using C++ may be good because it supports [raw string literal](https://en.cppreference.com/w/cpp/language/string_literal). – MikeCAT Feb 19 '21 at 13:03
  • zaro, perhaps you should read the input from a file then. – Ctx Feb 19 '21 at 13:04
  • `char text[] = "$$\";` is invalid C code. Anyway I don't get it you cannot change "$$\" to "$$\\"? Why not? It's not input, it's part of your code. Please clarify. – Jabberwocky Feb 19 '21 at 13:08
  • I meant that the input is given by a function in the coding website, that's why I cannot change it. – zaro Feb 19 '21 at 13:10
  • If the invalid line cannot be changed, how about hiding it from the compiler? The way is: 1. Surround the invalid line `char text[] = "$$\";` by `#if 0` and`#endif` (each should be on separate lines) 2. Add valid input `char text[] = "$$\\";` after that. – MikeCAT Feb 19 '21 at 13:10
  • That might work for this case, but I cannot manually do it for every other random case – zaro Feb 19 '21 at 13:11
  • 3
    It doesn't make sense that online juge systems will give such a invalid C code unless the problem is about implementing a compiler and detecting compilation error, or the system is broken. It seems we want the exact specification of the website. – MikeCAT Feb 19 '21 at 13:13
  • I also don't know why it is given that way and this is why I though I should maybe first make an if statement finding '\' character, adding 1 to the pointer size and then add a second '\' – zaro Feb 19 '21 at 13:15
  • zaro, does your program _generate_ the source code you show above? – Ctx Feb 19 '21 at 13:16
  • Adding 1 to pointer size doesn't make sense because the pointer size will be fixed by the system. – MikeCAT Feb 19 '21 at 13:17
  • No, I create the the whole code. The program only gives me the input [ char *DuplicateEncoder(char *text) ]. That's why I only think to enlarge the pointer and place a second '\' after the first one – zaro Feb 19 '21 at 13:19
  • 1
    I think your confusion is thinking that "\\" is two characters. It is a single backslash in C. strlen will be 1. – stark Feb 19 '21 at 13:30
  • No, I am not confusing it, but that is the only way the the compiler to read it as a sinlge \ and not give me error – zaro Feb 19 '21 at 13:34
  • 2
    "The input is fixed..." Nope, **the input is illegal C** – pmg Feb 19 '21 at 13:44
  • Is the input fixed, or is the line of code that you are using to attempt to initialize the array `text` fixed? If you are told that you need to work with the string consisting of 2 dollar signs and a backslash, you need to be aware that the line `char text[] = "$$\";` does not initialize the array to that string, so you are violating the rules. – William Pursell Feb 19 '21 at 14:13
  • The inputs are fixed and are given by a function; however, since I am doing the tasks in my IDE first, I simply create a string and place the test cases in it – zaro Feb 19 '21 at 14:30
  • @MikeCAT Code between `#if 0` and `#else` or `#endif` also needs to consist of valid preprocessing tokens, and `"$$\"` is not a valid preprocessing token. – Ian Abbott Feb 19 '21 at 15:06
  • @zaro So you created the invalid `"$$\"` string literal by yourself? Just change it to `"$$\\"`. `strlen("$$\\")` is 3. – Ian Abbott Feb 19 '21 at 15:09
  • No, "$$\" is passed by a function in the problem I am solving. Because I do it my IDE, not in the site itself, I create the string and copy the test cases into that string. That's why I cannot just simply add another '\'. – zaro Feb 19 '21 at 16:33

3 Answers3

1

Since we are already proposing trigraphs, you can alternatively also use a magic number instead (x86 solution):

#include <stdio.h>
int main(void) 
{ 
  char* text = (char*)&(int) <%6038564%>;
  puts(text);
}

Output:

$$\

Or if you like the magic number 027022044 better, then go for that one instead.

(Not really a serious answer, but it's Friday, so...)

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

Nothing can escape from an escape character, even an escape character....

In C, all escape sequences consist of two or more characters

The first of which is the backslash, \, called the Escape character.

The remaining characters determine the interpretation of the escape sequence.

Escape sequence of \\ equals 5C hex value in ascii which represents character of Backslash. Therefore in order to use backslash, you have to use it twice.

Sekomer
  • 688
  • 1
  • 6
  • 24
  • Yes, but I cannot modify the input, because it is fixed and given by a function. – zaro Feb 19 '21 at 13:26
  • You can not avoid escaping in C language, I am sorry it is not helping you but my answer is correct :) – Sekomer Feb 19 '21 at 13:28
0

You can use trigraph sequence

char text[] = "$$??/"; // { dollar-sign, dollar-sign, backslash, zero }
pmg
  • 106,608
  • 13
  • 126
  • 198