Jan 30, 2012

K & R C Programs Exercise 5-6.

K and R C, Solution to Exercise 5-6:
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 to Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline (Chapters 1 and 4), atoi , itoa , and their variants (Chapters 2, 3, and 4), reverse (Chapter 3), and strindex and getop (Chapter 4). 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 <ctype.h>
#include <stdio.h>
#include <stdlib.h>

  /* getline: get line into s, return length */
  int getline(char *s, int lim)
{
 char *p;
 int c;

 p = s;
 while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
  *p++ = c;
 if (c == '\n')
  *p++ = c;
 *p = '\0';
 return (int)(p - s);
}


int atoi(char *s)
{
 int n, sign;

 while (isspace(*s))
  s++;
 sign = (*s == '+' || *s == '-') ? ((*s++ == '+') ? 1 : -1) : 1;
 for (n = 0; isdigit(*s); s++)
  n = (n * 10) + (*s - '0');  
 return sign * n;
}

/*The itoa() function converts an integer value into an
ASCII string of digits.*/

char *utoa(unsigned value, char *digits, int base)
{
 char *s, *p;

 s = "0123456789abcdefghijklmnopqrstuvwxyz"; 
 if (base == 0)
  base = 10;
 if (digits == NULL || base < 2 || base > 36)
  return NULL;
 if (value < (unsigned) base) {
  digits[0] = s[value];
  digits[1] = '\0';
 } else {
  for (p = utoa(value / ((unsigned)base), digits, base);
    *p;
    p++);
  utoa( value % ((unsigned)base), p, base);
 }
 return digits;
}

char *itoa(int value, char *digits, int base)
{
 char *d;
 unsigned u; 

 d = digits;
 if (base == 0)
  base = 10;
 if (digits == NULL || base < 2 || base > 36)
  return NULL;
 if (value < 0) {
  *d++ = '-';
  u = -((unsigned)value);
 } else
  u = value;
 utoa(u, d, base);
 return digits;
}


static void swap(char *a, char *b, size_t n)
{
 while (n--) {
  *a ^= *b;
  *b ^= *a;
  *a ^= *b;
  a++;
  b++;
 }
}

void my_memrev(char *s, size_t n)
{
 switch (n) {
 case 0:
 case 1:
  break;
 case 2:
 case 3:
  swap(s, s + n - 1, 1);
  break;
 default:
  my_memrev(s, n / 2);
  my_memrev(s + ((n + 1) / 2), n / 2);
  swap(s, s + ((n + 1) / 2), n / 2);
  break;
 }
}

void reverse(char *s)
{
 char *p;

 for (p = s; *p; p++)
  ;
 my_memrev(s, (size_t)(p - s));
}



static char *strchr(char *s, int c)
{
 char ch = c;

 for ( ; *s != ch; ++s)
  if (*s == '\0')
   return NULL;
 return s;
}

int strindex(char *s, char *t)
{
 char *u, *v, *w;

 if (*t == '\0')
  return 0;
 for (u = s; (u = strchr(u, *t)) != NULL; ++u) {
  for (v = u, w = t; ; )
   if (*++w == '\0')
    return (int)(u - s);
   else if (*++v != *w)
    break;
 }
 return -1;
}


#define NUMBER '0'     

int getop(char *s)
{
 int c;

 while ((*s = c = getch()) == ' ' || c == '\t')
  ;
 *(s + 1) = '\0';
 if (!isdigit(c) && c != '.')
  return c;       /* not a number */
 if (isdigit(c))     /* collect integer part */
  while (isdigit(*++s = c = getch()))
   ;
 if (c == '.')       /* collect fraction part */
  while (isdigit(*++s = c = getch()))
   ;
 *++s = '\0';
 if (c != EOF)
  ungetch(c);
 return NUMBER;
}




Read more 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