C语言字符串转义函数实现 - 将换行符和制表符转换为\n\t
#include <stdio.h>
void escape(char s[], char t[]) { int i, j; for (i = 0, 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; } j++; } s[j] = '\0'; }
int main() { char t[51], s[101]; printf("请输入字符串t:"); fgets(t, 51, stdin); escape(s, t); printf("转换后的字符串s为:%s", s); return 0; }
原文地址: https://www.cveoy.top/t/topic/otPW 著作权归作者所有。请勿转载和采集!