简体   繁体   English

如何解决“\”字符的问题?

[英]How can I fix the problem with '\' character?

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 '\'?如果我添加第二个'\'很好,但是有没有一种快速的方法来修复它,或者我应该以某种方式将指针的大小加1,然后添加第二个'\'?

PS The input is fixed. PS 输入是固定的。 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;

Since we are already proposing trigraphs, you can alternatively also use a magic number instead (x86 solution):由于我们已经提出了三元组,您也可以使用幻数代替(x86 解决方案):

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

Output: Output:

$$\

Or if you like the magic number 027022044 better, then go for that one instead.或者,如果您更喜欢幻数027022044 ,那么可以使用 go 代替。

(Not really a serious answer, but it's Friday, so...) (不是很认真的答案,但现在是星期五,所以......)

Nothing can escape from an escape character, even an escape character....没有任何东西可以从转义字符中逃脱,即使是转义字符....

In C, all escape sequences consist of two or more characters在 C 中,所有转义序列都包含两个或多个字符

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 . \\的转义序列等于 ascii 中的5C十六进制值,表示Backslash的字符。 Therefore in order to use backslash, you have to use it twice.因此,为了使用反斜杠,您必须使用它两次。

You can use trigraph sequence您可以使用三字母序列

char text[] = "$$??/"; // { dollar-sign, dollar-sign, backslash, zero }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 C 中的 If 和结构问题? - How can I fix the If and structure problem in C? 我如何开始解决这个问题并修复我的代码? - How can I start with this problem and fix my code? 如何解决 C 中的 gets() function 的问题? - How can I fix the problem with gets() function in C? 在 C 中打印字符串时如何解决此问题? - How I can fix this problem when print string in C? 如何修复 C 代码中的指针问题? - How can I fix pointer problem in my C code? 数字字符串的最后一个字符被删除,如何解决 C 中的这个问题? - Last character of numeric string being removed, how to fix this problem in C? 如果我运行任何非字母顺序的字符,如何修复我的程序不崩溃? - How can I fix my program to not crash if I run any character that is not alphabetical? 如何修复警告:C4129 'e':无法识别的字符转义序列 - How can I fix Warning: C4129 'e': unrecognized character escape sequence 我将数组的旧元素保存在新数组中。 我该如何解决这个问题? - i save the old element of the array in the new array. how can i fix this problem? 这是我的代码的简化版本。 我对 output 有疑问。 我该如何解决? - It is simplified version of my code. I have a problem with output. How can I fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM