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
#include
//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("%sn", 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++] = ' ';
return l;
}
输入样例:
aaa sss
#include#include //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("%sn", 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++] = ' '; return l; }
输入样例:
aaa sss
结尾无空行
输出样例:
aaa sssn
结尾无空行
网站上有不少用switch case的答案,我这里就用if语句了
void escape(char a[], char b[]){
int i,j;
for(i=0,j=0;b[i]!=' ';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]=' ';
}



