[英]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.