Write a function escape(s,t) that converts characters like newline and tab into visible escape sequences like \n and \t as it copies the string t to s.
Use a switch.
函数接口定义:
void escape(char [], char []);
裁判测试程序样例:
#include <stdio.h>
#include <assert.h>
//using namespace std;
//constexpr int N = 100001;
const int N = 100001;
void escape(char [], char []);
int getline(char []);
int main()
{
char s[N * 2], t[N];
while(getline(t) != EOF) {
escape(s, t);
printf("%s\n", s);
}
return 0;
}
int getline(char t[])
{
int l = 0;
char c;
while(l + 2 <= N && (c = getchar()) != '\n' && c != EOF)
t[l++] = c;
if(c == EOF) return EOF;
assert(l + 2 <= N);
t[l++] = '\n';
t[l++] = '\0';
return l;
}
/* 请在这里填写答案 */
输入样例:
aaa sss
结尾无空行
输出样例:
aaa sss\n
结尾无空行
网站上有不少用switch case的答案,我这里就用if语句了
void escape(char a[], char b[]){
int i,j;
for(i=0,j=0;b[i]!='\0';i++,j++){
if(b[i]=='\t'){
a[j]='\\';
j++;
a[j]='t';
}
else if(b[i]=='\n'){
a[j]='\\';
j++;
a[j]='n';
}
else a[j]=b[i];
}
a[j+1]='\0';
}