# K & R C Programs Exercise 3-5.

K and R C, Solution to Exercise 3-5:
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.
Write a c program to convert the integer n into a base b character representation in the string s. In particular, itob(n, s, 16) formats n as a hexadecimal integer in s. 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 <stdlib.h>#include <stdio.h>void itob(int n, char s[], int b);void reverse(char s[]);int main(void) { char buffer[10]; int i; for ( i = 2; i <= 20; ++i ) {  itob(255, buffer, i);  printf("Decimal 255 in base %-2d : %sn", i, buffer); } return 0;}/*  itob: convert n to charecters in s - base b */void itob(int n, char s[], int b) { int i, j, sign; void reverse(char s[]); if ((sign = n) < 0)  n = -n; i = 0; do {  j = n%b;  s[i++] = (j <= 9) ? j+'0' : j+'a'-10; } while ((n /= b) > 0); if (sign < 0)  s[i++] = '-'; s[i] = ''; reverse(s);}/*  Reverses string s[] in place  */void reverse(char s[]) { int c, i, j; for ( i = 0, j = strlen(s)-1; i < j; i++, j--) {  c = s[i];  s[i] = s[j];  s[j] = 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

# 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 [email protected]* 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,ntWorld! Mistakeeb was "Extra 'e'"!n"; char text2[51]; printf("Original string:n%sn", text1); escape(text2, text1); printf("Escaped string:n%sn", text2); unescape(text1, text2); printf("Unescaped string:n%sn", 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 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

# K & R C Programs Exercise 3-1.

K and R C, Solution to Exercise 3-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 implement binary Search. Binary search technique is simple searching technique which can be applied if the items to be compared are either in ascending order or descending order. The general idea used in binary search is similar to the way we search for the telephone number of a person in the telephone directory. Binary search is the divide and conquer strategy. 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***********************************************************//*C Program for Binary search */#include<stdio.h>int main() { int n, a[30], item, i, j, mid, top, bottom; printf("Enter how many elements you want:n"); scanf("%d", &n); printf("Enter the %d elements in ascending ordern", n); for (i = 0; i < n; i++) {  scanf("%d", &a[i]); } printf("nEnter the item to  searchn"); scanf("%d", &item); bottom = 1; top = n; do {  mid = (bottom + top) / 2;  if (item < a[mid])   top = mid - 1;  else if (item > a[mid])   bottom = mid + 1; } while (item != a[mid] && bottom <= top); if (item == a[mid]) {  printf("Binary search successfull!!n");  printf("n %d found in position: %dn", item, mid + 1); } else {  printf("n  Search failedn %d not foundn", item); } return 0;}`
`Read more Similar C ProgramsC BasicC Search AlgorithmsK and R C Programs Exercise`

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