- 论坛徽章:
- 0
|
本帖最后由 xiaowh00 于 2012-06-19 17:02 编辑
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. Write a function for the other direction as well, converting escape sequences into the real characters
完成了第一个函数 把不可见的tab和换行转换成\t和\n,可以从键盘输入或者文件输入
#include <stdio.h>
#define MAX 10000
/* author: xiaowh00 */
/* date: 2012-6-19 */
/*
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. Write a function for the other direction as well, converting escape sequences into the real characters
*/
void escape(char [],char []);
int main(int argc,char *argv[])
{
FILE *fp;
char s[MAX],t[MAX];
int i,j,c;
i=0;
if(argc==1)
{
while((c=getchar())!=EOF)
t[i++]=c;
t='\0';
escape(s,t);
printf("%s\n",s);
}
else
{
j=0;
while(++j<argc)
{
if((fp=fopen(argv[j],"r"))==NULL)
printf("Cant not open the file %s\n",argv[j]);
c=fgetc(fp);
while(c!=EOF)
{
t[i++]=c;
c=fgetc(fp);
}
t='\0';
escape(s,t);
printf("File %s result:>>>\n\n%s\n",argv[j],s);
fclose(fp);
}
}
}
void escape(char s[],char t[])
{
int i,j;
for(i=0,j=0;t!='\0';i++)
{
switch(t)
{
case '\n': s[j++]='\\';s[j++]='n';break;
case '\t': s[j++]='\\';s[j++]='t';break;
default: s[j++]=t;break;
}
}
} |
|