编写一个函数escape(s,t),当它将字符串t复制到s时,将换行符和制表符之类的字符转换为可见的转义序列,如\n和\t。

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';
 
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值