C语言在字符串中单引号前面加上转义字符函数
C语言中单引号前面加上转义字符的函数是:\
void escape_char(char* str) {
int i, j;
char temp[100];
for (i = 0, j = 0; str[i] != '\0'; i++, j++) {
if (str[i] == '\'') {
temp[j++] = '\\';
}
temp[j] = str[i];
}
temp[j] = '\0';
strcpy(str, temp);
}
该函数接收一个字符串作为参数,然后遍历该字符串中的每个字符,如果遇到单引号,则在该单引号前面加上一个反斜杠(\)转义字符,最后将转义后的字符串复制回原字符串中。
原文地址: http://www.cveoy.top/t/topic/btBt 著作权归作者所有。请勿转载和采集!