2014年05月15日

时间:2021-05-30 11:50:39

practice 3-2:Write a function escape(s,t),copy string t to string s,and display some invisable charactor like '\n' '\t' and so on,make them visable,with switch sentence grammer.Then write another function which has a opposite affect,that is to turn those charactor to be invisable.

#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\b was \"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 within procesures is the most tough job for me ,and it takes lots of my time .A long time ago,I will choose to give up or give in to it.But now, I reallize what can make me feel a little of successful is not the right answers do I give but the difficult in the process do I conquer.More questions,more exciting!So,let's move on,and welcome troubles.