2014年05月15日

来源:互联网 发布:呱呱社区软件 编辑:程序博客网 时间:2024/04/26 09:35

practice 3-2:Write a functionescape(s,t),copy string t to string s,and display some invisablecharactor like '\n' '\t' and so on,make them visable,with switchsentence grammer.Then write another function which has a oppositeaffect,that is to turn those charactor to beinvisable.

#include
#define MAXLINE 100

void escape(char s[],char t[])
{
 int i,j=0;
 for(i=0;t[i]!='\0';++i)
 {
  switch(t[i])
  {
  case '\t':
   s[j++]='\\';
   s[j]='t';
   ++j;
   break;
  case '\n':
   s[j++]='\\';
   s[j]='n';
   ++j;
   break;
  default:
   s[j]=t[i];
   ++j;
   break;
  }
 }
 s[j]='\0';


}
void  inverse(char t[],char s[])
{
 int i,j=0;

 for(i=0;s[i]!='\0';++i)
 {
  switch(s[i])
  {
   case'\\':
    {
    switch(s[i+1])
    {
     case't':
      t[j++]='\t';
      ++i;
      break;
     case'b':
      t[j++]='\b';
      ++i;
      break;
     case'n':
      t[j++]='\n';
      ++i;
      break;
     default:
      break;
    }
    break;
    }
   default:
    t[j++]=s[i];
    
    break;
  }
 }
 t[j]='\0';

 
}
main()
{
 char s1[MAXLINE]="\aHello,\n\tWorld! Mistakee\bwas \"Extra 'e'\"!\n";
 char s2[MAXLINE];
 
 printf("the original string:\n%s\n",s1);

 escape(s2,s1);
 printf("escape string:\n%s\n",s2);
 
 inverse(s1,s2);
 printf("inverse string:\n%s\n",s1);

 return 0; 
}

 

Conclusion:To find out what it is wrong withinprocesures is the most tough job for me ,and it takes lots of mytime .A long time ago,I will choose to give up or give in to it.Butnow, I reallize what can make me feel a little of successful is notthe rightanswers do I give butthe difficult in the process do Iconquer.More questions,more exciting!So,let's move on,and welcometroubles.

0 0
原创粉丝点击