C语言字符串转义函数实现 - 将换行符和制表符转换为转义序列
以下是代码实现:
#include <stdio.h>
void escape(char s[], char t[]) {
int i, j;
for (i = j = 0; t[i] != '\0'; i++) {
switch (t[i]) {
case '\n':
s[j++] = '\';
s[j++] = 'n';
break;
case '\t':
s[j++] = '\';
s[j++] = 't';
break;
default:
s[j++] = t[i];
break;
}
}
s[j] = '\0';
}
int main() {
char t[100], s[100];
printf("请输入字符串:");
fgets(t, 100, stdin);
escape(s, t);
printf("转换后的字符串为:%s", s);
return 0;
}
运行程序后,输入字符串,即可得到对应的转换后的字符串。例如,输入字符串'Hello\tworld\n',则输出的转换后的字符串为'Hello\tworld\n'。
原文地址: https://www.cveoy.top/t/topic/o0kG 著作权归作者所有。请勿转载和采集!