Jan 2, 2012

K & R C Programs Exercise 3-2.

K and R C, Solution to Exercise 3-2:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program that converts the characters like newline and tab into visible escape sequences like \n and \t as it copies the string t to s. Use a Switch case, and Write the function for the other direction as well, converting escape sequences into the real characters. Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>

void escape(char * s, char * t);
void unescape(char * s, char * t);

int main(void) {
 char text1[50] = "\aHello,\n\tWorld! Mistakee\b was \"Extra 'e'\"!\n";
 char text2[51];

 printf("Original string:\n%s\n", text1);

 escape(text2, text1);
 printf("Escaped string:\n%s\n", text2);

 unescape(text1, text2);
 printf("Unescaped string:\n%s\n", text1);

 return 0;
}

/*escape: expand newline and tab and others tabs into visible sequences using switch*/

void escape(char * s, char * t) {
 int i, j;
 i = j = 0;

 while ( t[i] ) {



  switch( t[i] ) {
  case '\n':
  s[j++] = '\\';
  s[j] = 'n';
  break;

  case '\t':
   s[j++] = '\\';
   s[j] = 't';
   break;

  case '\a':
   s[j++] = '\\';
   s[j] = 'a';
   break;

  case '\b':
   s[j++] = '\\';
   s[j] = 'b';
   break;

  case '\f':
   s[j++] = '\\';
   s[j] = 'f';
   break;

  case '\r':
   s[j++] = '\\';
   s[j] = 'r';
   break;

  case '\v':
   s[j++] = '\\';
   s[j] = 'v';
   break;

  case '\\':
   s[j++] = '\\';
   s[j] = '\\';
   break;

  case '\"':
   s[j++] = '\\';
   s[j] = '\"';
   break;

  default:



   s[j] = t[i];
   break;
  }
  ++i;
  ++j;
 }
 s[j] = t[i];    
}


/*  unescape: convert escape sequences into real charecters while copying the string t to s      */

void unescape(char * s, char * t) {
 int i, j;
 i = j = 0;

 while ( t[i] ) {
  switch ( t[i] ) {
  case '\\':


   switch( t[++i] ) {
   case 'n':
    s[j] = '\n';
    break;

   case 't':
    s[j] = '\t';
    break;

   case 'a':
    s[j] = '\a';
    break;

   case 'b':
    s[j] = '\b';
    break;

   case 'f':
    s[j] = '\f';
    break;

   case 'r':
    s[j] = '\r';
    break;

   case 'v':
    s[j] = '\v';
    break;

   case '\\':
    s[j] = '\\';
    break;

   case '\"':
    s[j] = '\"';
    break;

   default:



    s[j++] = '\\';
    s[j] = t[i];
   }
   break;

   default:



    s[j] = t[i];
  }
  ++i;
  ++j;
 }
 s[j] = t[i];    
}


Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link