# K & R C Programs Exercise 5-7.

K and R C, Solution to Exercise 5-7:
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 the readline function to store lines in the arraysupplied by main, rather than calling alloc to maintain storage. How much faster is the program?
The readlile function is slightly faster than the original version in the book K and R C Program page 109.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 [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include <stdio.h>#include <string.h>#include <stdlib.h>#include <time.h>#define TRUE     1#define FALSE    0#define MAXLINES 5000       /* maximum number of lines */#define MAXLEN   1000       /* maximum length of a line */char *lineptr[MAXLINES];char lines[MAXLINES][MAXLEN];int getline(char s[], int lim){ int c, i; for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != 'n'; i++)  s[i] = c;                                                          if (c == 'n') {  s[i++] = c;    } s[i] = ''; return i;}int readlines(char *lineptr[], int maxlines){ int len, nlines; char *p, line[MAXLEN]; nlines = 0; while ((len = getline(line, MAXLEN)) > 0)  if (nlines >= maxlines || (p = malloc(len)) == NULL)   return -1;  else {   line[len - 1] = '';  /* delete the newline */   strcpy(p, line);   lineptr[nlines++] = p;  }                        return nlines;  }int readlines2(char lines[][MAXLEN], int maxlines){ int len, nlines; nlines = 0; while ((len = getline(lines[nlines], MAXLEN)) > 0)  if (nlines >= maxlines)                            return -1;             else   lines[nlines++][len - 1] = '';   return nlines;}int main(int argc, char *argv[]){ readlines2(lines, MAXLINES); if (argc > 1 && *argv[1] == '2') {  puts("readlines2()");             readlines2(lines, MAXLINES); } else {  puts("readlines()");  readlines(lineptr, MAXLINES); } return 0;}`
`Read more c programs `
`C BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

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

our page!

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

To browse more C Programs visit this link
(c) www.c-program-example.com

# 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 [email protected]* 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 = ''; 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 anASCII 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] = ''; } 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 == '')   return NULL; return s;}int strindex(char *s, char *t){ char *u, *v, *w; if (*t == '')  return 0; for (u = s; (u = strchr(u, *t)) != NULL; ++u) {  for (v = u, w = t; ; )   if (*++w == '')    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) = ''; 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 = ''; if (c != EOF)  ungetch(c); return NUMBER;}`
`Read more c programs `
`C BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

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

our page!

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

To browse more C Programs visit this link
(c) www.c-program-example.com

# K & R C Programs Exercise 5-1.

K and R C, Solution to Exercise 5-1:
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 get next integer from input into space, so that getint treats a + or – not followed by a digit as a valid representation of zero and fix it to push such a character back on the input. 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 [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/include<ctype.h>int getch(void);void ungetch(int);/* getint:  get next integer from input into *pn */int getint(int *pn){ int c, sign, sawsign; while (isspace(c = getch()))   /* skip white space */  ; if (!isdigit(c) && c != EOF && c != '+' && c != '-') {  ungetch(c);    /* it's not a number */  return 0; } sign = (c == '-') ? -1 : 1; if (sawsign = (c == '+' || c == '-'))  c = getch(); if (!isdigit(c)) {  ungetch(c);  if (sawsign)   ungetch((sign == -1) ? '-' : '+');  return 0; } for (*pn = 0; isdigit(c); c = getch())  *pn = 10 * *pn + (c - '0'); *pn *= sign; if (c != EOF)  ungetch(c); return c;}`
`Read more Similar C ProgramsC BasicC StringsK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

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

our page!

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

To browse more C Programs visit this link
(c) www.c-program-example.com

# C Program: Array Summation Using Pointers

Arrays in C.
Write a C program to read N integers and store them in an array A, and so find the sum of all these elements using pointer. Output the given array and and the computed sum with suitable heading. 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 [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                       Happy Coding***********************************************************/    #include <stdio.h>#include <conio.h>#include <malloc.h>void main(){ int i,n,sum=0; int *a; clrscr(); printf("Enter the size of array An"); scanf("%d", &n); a=(int *) malloc(n*sizeof(int));   /*Dynamix Memory Allocation */ printf("Enter Elements of First Listn"); for(i=0;i<n;i++) {  scanf("%d",a+i); } /*Compute the sum of all elements in the given array*/ for(i=0;i<n;i++) {  sum = sum + *(a+i); } printf("Sum of all elements in array = %dn", sum);}     /* End of main() */`
`Read more Similar C ProgramsArray In CSimple C Programs`

You can easily select the code by double clicking on the code area above.