## Jan 6, 2012

### C Program implement Kruskal's algorithm.

Write a C Program implement Kruskal's algorithm.
Kruskal's algorithm is a greedy algorithm that finds the minimum spanning tree of a graph. Graph should be weighted, connected, and undirected.Minimum spanning tree is a spanning tree with weight less than or equal to the weight of every other spanning tree. 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>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
clrscr();
printf("\n\n\tImplementation of Kruskal's algorithm\n\n");
printf("\nEnter the no. of vertices\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("\nThe edges of Minimum Cost Spanning Tree are\n\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
```
```Read more Similar C Programs
Data Structures
Dijkstras Algorithm

Learn C Programming```

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

## Jan 5, 2012

### K & R C Programs Exercise 4-1.

K and R C, Solution to Exercise 4-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.
Write a C program which returns the position of the occurrence of sub string t in string s, or -1 if there is none.

```/***********************************************************
* 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>
#include<conio.h>
void main()
{
int flag=0;
char str[80],search[10];
puts("Enter a string:");
gets(str);

puts("Enter search substring:");
gets(search);
flag=strindex(str, search);
if (flag == -1)
{
printf("SEARCH UNSUCCESSFUL! AND POSITION IS:%d",flag);
}
else{
printf("SEARCH SUCCESSFUL! AND POSITION IS:%d",flag);
}
getch();
}

//strindex: returns the right most index of t in s, -1 if none*/
int strindex(char s[], char t[])
{
int k,i,j,pos;
pos = -1;
for(i=0;s[i] != '\0'; i++)
{
for(j=i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
;
if (k > 0 && t[k] == '\0')
pos=i;
}
return pos;
}
```
```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.

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 program to implement bit flipping.

Write a C program to implement bit flipping.
C Program to setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged in the least number of lines.
Bit flipping or flip bit is the complement(~) of bits. i.e., 0 to 1 and vise versa. bit flipping is the bits manipulation or processing individual bits within a byte. Bit flipping is used in the very low-level programming and is often used in graphics and systems programming.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>
#include <math.h>
#include <limits.h>

unsigned setbits(unsigned x, unsigned p, unsigned n, unsigned y) {
x |= (y & ~(~0 << n)) << p;
size_t s = (int)(log(INT_MAX)/log(2)) + 1;
printf("\n\n\nSuccess! Number after bit flipping: \n", s);
do {
((x & mask) == 0) ? printf("0") : printf("1");
((s%4)==0) ? printf(" ") : printf("");
x <<= 1;
} while (s--);
printf("\n");
}

void main( ) {
unsigned retrn=1, begin=0, nbits=0, num=0;

printf("Enter the number to bit flip!\n");
scanf("%d",&num);
printf("Enter the number of bits to be flip!\n");
scanf("%d",&nbits);
printf("Enter the position to begin bit flip!\n");
scanf("%d",&begin);
unsigned x = setbits(retrn, begin, nbits, num);

}
```
```Read more Similar C Programs
C Basic
C Search Algorithms
K 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

## Jan 4, 2012

### C Program to print factors of the number.

Write a C Program to print factors of a number. Factors of a whole number are the whole numbers which are exactly divides the number. i.e reminder is equal to zero.
Example: Factors of 8 are: 1, 2, 4, and 8.

```/***********************************************************
* 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>
#include<conio.h>
{
void main( )
{
int num,x;
clrscr( );
printf("Enter the required number:");
scanf("%d",&num);
printf("\nThe factors are:");
for(x=1; x<=num; x++)
{
if(num%x==0)
printf("\n%d",x);
}
getch( );
}
```
```Read more Similar C Programs
C Basic
C Search Algorithms
K 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 Program to implement topological sort.

Write a c program to implement topological sort.
Topological sort is the ordering vertices of a directed, acyclic graph(DAG), so that if there is an arc from vertex i to vertex j, then i appears before j in the linear ordering. 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>
#define MAX 200
int front = -1,rear = -1,queue[MAX];
void main()
{
int i,j = 0,k;
int topsort[MAX],indeg[MAX];
create_graph();
display();
for(i=1;i<+n;i++)
{
indeg[i]=indegree(i);
if(indeg[i]==0)
insert_queue(i);
}
while(front<=rear)
{
k=delete_queue();
topsort[j++]=k;
for(i=1;i<=n;i++)
{
{
indeg[i]=indeg[i]-1;
if(indeg[i]==0)
insert_queue(i);
}
}
}
printf("Nodes after topological sorting are:\n");
for(i=0;i<=n;i++)
printf("%d",topsort[i]);
printf("\n");
}
create_graph()
{
int i,max_edges,origin,destin;
printf("\n Enter number of vertices:");
scamf("%d",&n);
max_edges = n * (n - 1);
for(i = 1;i <= max_edges;i++)
{
printf("\n Enter edge %d (00 to quit):",i);
scanf("%d%d",&origin,&destin);
if((origin == 0) && (destin == 0))
{
printf("Invalid edge!!\n");
i–;
}
else
}return;
}
display()
{
int i,j;
for(i = 0;i <= n;i++)
{
for(j = 1;jrear)
{
printf(“Queue Underflow”);
return;
}
else
{
del_item = queue[front];
front = front + 1;
return del_item;
}
}
int indegree(int node)
{
int i,in_deg = 0;
for(i = 1;i <= n;i++)
in_deg++;
returnin_deg;
}

```
Data Structures
C Sorting
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

### K & R C Programs Exercise 3-6.

K and R C, Solution to Exercise 3-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 change version of itoa that accepts three arguments instead of two(K and R C Exercise 3-4). The third argument is a minimum field width; the converted number must be paddle with blanks on the left if necessary to make it wide enough. 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 <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define abs(x)  ((x) < 0 ? -(x) : (x))
void itoa(int n, char s[], int w);
void reverse(char s[]);

int main(void) {
char buffer[20];

printf("INT_MIN: %d\n", INT_MIN);
itoa(INT_MIN, buffer);
printf("Buffer : %s\n", buffer);

return 0;
}
//itoa: convert to n characters in s, w characters wide.
void itoa(int n, char s[], int w)
{
void int i, sign;
void everse(char s[]);
sign = n;
i = 0;
do{
s[i++] = abs(n%10) + '0';
printf("%d %% %d + '0' = %d\n", n, 10, s[i-1]);
}while((n/=10)!=0);
if(sign < 0)
s[i++] = '-';
while(i < w)
s[i++] = ' ';
s[i] = '\0';
reverse(s);
}
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 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.

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

### 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 info@c-program-example.com
* 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 : %s\n", 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] = '\0';
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 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.

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

### K & R C Programs Exercise 3-4.

K and R C, Solution to Exercise 3-4:
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 represent the binary numbers in a negative sign.There are a number of ways of representing signed integers in binary, for example, signed-magnitude, excess-M, one's complement and two's complement. In this program , we handle the problem that is, the value of n equal to -(2 to the power (wordsize - 1))(C Programming Language (2nd Edition) by Brian W.Kernighan & Dennis M.Richie page no 64) by changing the (n /= 10) > 0 to (n /= 10) != 0. 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 <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define abs(x)  ((x) < 0 ? -(x) : (x))
void itoa(int n, char s[]);
void reverse(char s[]);

int main(void) {
char buffer[20];

printf("INT_MIN: %d\n", INT_MIN);
itoa(INT_MIN, buffer);
printf("Buffer : %s\n", buffer);

return 0;
}
//itoa: convert to n characters in s - modified.
void itoa(int n, char s[])
{
void int i, sign;
void everse(char s[]);
sign = n;
i = 0;
do{
s[i++] = abs(n%10) + '0';
}while((n/=10)!=0);
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
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 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.

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

## Jan 3, 2012

### C Program to get the system information.

C Program to get the system information. We can use the 'uname' function to get the system information, which is defined in the "sys/utsname.h" library.Structure is used to hold information returned by the uname function.
"uname" function members and their use:
sysname returns the name of the operating system in use.
release returns the current release level of the operating system implementation.
version returns the current version level within the release of the operating system.
machine is the description of the type of hardware that is in use.

```/***********************************************************
* 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<sys/utsname.h>   /* Header for 'uname'  */

main()
{
struct utsname uname_pointer;

uname(&uname_pointer);

printf("System name - %s \n", uname_pointer.sysname);
printf("Nodename    - %s \n", uname_pointer.nodename);
printf("Release     - %s \n", uname_pointer.release);
printf("Version     - %s \n", uname_pointer.version);
printf("Machine     - %s \n", uname_pointer.machine);
printf("Domain name - %s \n", uname_pointer.domainname);
}

```
```Read more Similar C Programs
Learn C Programming

Number System```

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 Program to reverse a Linked List.

Data structures using C,
C Program to reverse a Linked List. Linked list is a data structure in which the objects are arranged in a linear order. Linked List contains group of nodes, in which each node contains two fields, one is data field and another one is the reference field which contains the address of next node. In this program, We reverse the linked list, but without sorting the linked list.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>
#include <stdlib.h>

#define MAX 100

struct leftnode {
int num;
struct leftnode *next;
};

int main(void) {
struct leftnode *new = NULL;
int i = 0;

for(i = 0; i <= MAX; i++)

return 0;
}

struct leftnode *temp = NULL;
temp = malloc(sizeof(struct leftnode));
temp->num = val;
temp->next = *n;
*n = temp;
}

struct leftnode *a = NULL;
struct leftnode *b = NULL;
struct leftnode *c = NULL;
a = *n, b = NULL;

while(a != NULL) {
c = b, b = a, a = a->next;
b->next = c;
}

*n = b;
}

while(n != NULL)
printf(" %d", n->num), n = n->next;

printf("\n");
}

```
```Read more Similar C Programs
Data Structures

Learn C Programming```
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 Program to implement Integration.

C Program to implement Integration. Integration is the important concept of calculus, which is the inverse of the differentiation. A definite integral gives the area between the graph of a function and the horizontal axis between vertical lines at the endpoints of an interval. Integration is used to find the area, volume of irregular shapes. In the following code, We find the integration of the defined function between two boundary limits, which are entered by the user. 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>
/* Propram to perform definite integration of a given function between
two boundary limits input by user. Feel free to use and modify it, but
please do not remove this comment.
source: C for Engineering, http://c4engineering.hypermart.net */

#define N 1000

void main(void) {
float i, a, b, sum = 0;
printf(
"\nThis program will integrate a function between two boundary limits.");
printf("\n\nEnter the first boundary limit:");
scanf("%f", &a);
printf("\nEnter the second boundary limit:");
scanf("%f", &b);
if (a > b) {
i = a;
a = b;
b = i;
}

for (i = a; i < b; i += (b - a) / N) {
/* Define your function below, and include the suitable header files */
y = x * x + 2 * x - 4;
sum += y * (b - a) / N;
}

printf("\n\nValue of integration is:%.3f", sum);
getch();
return;
}

```
```Array In C

C Data Structures

Simple C Programs```
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

### K & R C Programs Exercise 3-3.

K and R C, Solution to Exercise 3-3:
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 expand short hand notations like a-z in the string s1 into the equivalent complete list abc---xyz in s2. Allowed for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. Arranged that a leading or trailing - is taken literally. 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
#include

void expand(char * s1, char * s2);

int main(void) {
char *s[] = { "a-z-", "z-a-", "-1-6-",
"a-ee-a", "a-R-L", "1-9-1",
"5-5", NULL };
char result[100];
int i = 0;

while ( s[i] ) {

expand(result, s[i]);
printf("Unexpanded: %s\n", s[i]);
printf("Expanded  : %s\n", result);
++i;
}

return 0;
}

//expand: expand short hand notations in s1 into string s2
void expand(char * s1, char * s2) {
static char upper_alph[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static char lower_alph[27] = "abcdefghijklmnopqrstuvwxyz";
static char digits[11]     = "0123456789";

char * start, * end, * p;
int i = 0;
int j = 0;

/*  Loop through characters in s2  */

while ( s2[i] ) {
switch( s2[i] ) {
case '-':
if ( i == 0 || s2[i+1] == '\0' ) {

s1[j++] = '-';
++i;
break;
}
else {

if ( (start = strchr(upper_alph, s2[i-1])) &&
(end   = strchr(upper_alph, s2[i+1])) )
;
else if ( (start = strchr(lower_alph, s2[i-1])) &&
(end   = strchr(lower_alph, s2[i+1])) )
;
else if ( (start = strchr(digits, s2[i-1])) &&
(end   = strchr(digits, s2[i+1])) )
;
else {

fprintf(stderr, "EX3_3: Mismatched operands '%c-%c'\n",
s2[i-1], s2[i+1]);
s1[j++] = s2[i-1];
s1[j++] = s2[i++];
break;
}

p = start;
while ( p != end ) {
s1[j++] = *p;
if ( end > start )
++p;
else
--p;
}
s1[j++] = *p;
i += 2;
}
break;

default:
if ( s2[i+1] == '-' && s2[i+2] != '\0' ) {

++i;
}
else {

s1[j++] = s2[i++];
}
break;
}
}
s1[j] = s2[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.

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

## 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.

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

### 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 info@c-program-example.com
* 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 order\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nEnter the item to  search\n");
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: %d\n", item, mid + 1);
} else {
}
return 0;
}
```
```Read more Similar C Programs
C Basic
C Search Algorithms
K 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

### K & R C Programs Exercise 2-10.

K and R C, Solution to Exercise 2-10:
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 Function lower, which converts the lower case, with a conditional expression instead of if-else.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
***********************************************************/

int lower(int c)
{
return c >= 'A' && c<= 'Z' ? c + 'a' - 'A': c;
}
```
```Read more Similar C Programs
C Basic

K 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

### K & R C Programs Exercise 2-9.

K and R C, Solution to Exercise 2-9:
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.
In C two complement number system, x &= (x-1) deletes the rightmost one bit in x.
We take (x-1) and add 1 to it to produce x. The rightmost 0-bit of x-1 changes to 1 in the result x. Therefore, the rightmost 1-bit of x has a corresponding 0-bit in x-1. This is why x & (x-1), in a two's complement number system, will delete the rightmost 1-bit in x. 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
***********************************************************/
int bitcount(unsigned x)
{
int b;
for(b = 0;x != 0;x &= x-1)
++b;
return b;
}
```
```Read more Similar C Programs
C Basic

K 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

### K & R C Programs Exercise 2-8.

K and R C, Solution to Exercise 2-8:
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 returns the value of the integer x rotated to the right by n bit positions. 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
***********************************************************/

unsigned rightroot(unsigned x, int n)
{
int wordlength(void);
int rbit;           //rightmost bit

while(n-->0){
rbit=(x & 1)<<(wordlength()-1);
x=x>>1;
x=x|rbit;
}
return x;
//word length computes the wor lengt of machine
int wordlength()
{
int i;
unsigned v = (unsigned~0;
for(i = 1;(v = v >> 1) > 0;i++)
;
return i;
}
//main function to test the program,, you can try in different ways!
#include <stdio.h>

int main(void)
{
unsigned x;
int n;

for(x = 0; x < 200; x += 25)
for(n = 1; n < 8; n++)
printf("%u, %d: %u\n", x, n, rightrot(x, n));
return 0;
}

```
```Read more Similar C Programs
C Basic

K 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

### K & R C Programs Exercise 2-7.

K and R C, Solution to Exercise 2-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 function invert(x, p, n) that returns x with the n bits that begin at position p inverted(i.e, 1 changed into 0 and vice versa), leaving the others unchanged.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
***********************************************************/

unsigned invert(unsigned x, int p, int n)
{
return x ^ (~(~0U << n) << p);
}

/* Program for testing */

#include <stdio.h>

int main(void)
{
unsigned x;
int p, n;

for(x = 0; x < 500; x += 49)
for(n = 1; n < 8; n++)
for(p = 1; p < 8; p++)
printf("%u, %d, %d: %u\n", x, n, p, invert(x, n, p));
return 0;
}

```
```Read more Similar C Programs
C Basic

K and R C Programs Exercise```

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