c语言编写程序编写函数escapest将字符串t拷贝到字符串s中并在拷贝过程中将诸如换行符与制表符转换成诸如nt等换码序列编写一个程序将输入的字符串含有换行符制表符进行转换并输出
以下是代码实现:
#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/hxyW 著作权归作者所有。请勿转载和采集!