在将字符串s复制到字符串t时,将其中的换行符和制表符转换为可见的转义字符

2025-03-20 19:12:39
推荐回答(1个)
回答1:

#include 
//复制拷贝s1到s2;
void changecopy(char *s1,char *s2){
int p1,p2;
for(p1=p2=0;s1[p1]!=0;p1++,p2++){
if (s1[p1]=='\n'){
s2[p2++]='\\';
s2[p2]='n';
}
else if (s1[p1]=='\t'){
s2[p2++]='\\';
s2[p2]='t';
}
else s2[p2]=s1[p1];
}
s2[p2]=0;
}
int main(){
char s1[]="hello\n,hello\t,hello";
char s2[100];
printf("s1=%s\n",s1);
changecopy(s1,s2);  //复制拷贝s1到s2; 
printf("s2=%s\n",s2);
}