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 [email protected]
* 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: %dn", INT_MIN);
itoa(INT_MIN, buffer);
printf("Buffer : %sn", 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] = '';
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.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

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

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

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 [email protected]
* 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;
};


void linklist_add(struct leftnode **n, int val);

void linklist_reverse(struct leftnode **n);

void linklist_display(struct leftnode *n);

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

for(i = 0; i <= MAX; i++)
linklist_add(&new, i);

printf("Linked list before reversal:n");
linklist_display(new);
linklist_reverse(&new);
printf("Linked list after reversal:n");
linklist_display(new);

return 0;
}


void linklist_add(struct leftnode **n, int val) {
struct leftnode *temp = NULL;
temp = malloc(sizeof(struct leftnode));
temp->num = val;
temp->next = *n;
*n = temp;
}


void linklist_reverse(struct leftnode **n) {
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;
}

void linklist_display(struct leftnode *n) {
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.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

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 [email protected]
* 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("nnEnter 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("nnValue 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.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

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 [email protected]
* 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: %sn", s[i]);
printf("Expanded : %sn", 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] == '' ) {

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] != '' ) {

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

To get regular updates on new C programs, you can Follow @c_program

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
(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 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 Follow @c_program

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

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

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 [email protected]
* 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.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

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 [email protected]
* 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.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

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 [email protected]
* 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: %un", 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.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com