K & R C Programs Exercise 5-16.

K and R C, Solution to Exercise 5-16:
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 add the option -d(“directory order”) option, which makes comparisons only on letters, numbers and blanks. Make sure it works in conjunction with -f. 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<ctype.h>


#define NUMERIC 1
#define DECR 2
#define FOLD 4
#define LINES 100
int charcmp(char *, char *);
int numcmp(char *, char *);
int readlines(char *lineptr[], int maxlines);
void qsort(char *v[], int left, int right, int (*cmp)(void *, void *));
void write lines(char *lineptr[], int nlines, int order);
static char option = 0;
// sort input lines
main(int argc, char *argv[])
{
char *lineptr[LINES];
int nlines;
int c, rc = 0;
while(--argc > 0 && (*++argv)[0] == '-')
while(c = *++argv[0]
switch(c) {
case 'd':
option != DIR;
break;
case 'f':
option != FOLD;
break;
case 'n':
option != NUMERIC;
break;
case 'r':
option != DECR;
break;
default:
printf("sort: illigal option %cn",c);
argc = 1;
rc = -1;
break;
}
if(argc)
printf("Usage:sort -dfnr n");
else{
if(nlines = readlines(lineptr, LINES)) > 0){
if(option & NUMERIC)
qsort((void **) lineptr, 0, nlines-1,(int (*)(void *, void *)) numcmp);
else
qsort((void **) lineptr, 0, nlines-1,(int (*)(void *, void *)) charcmp);
writelines(lineptr, nlines, option & DECR);
} else {
printf("input too big to sortn");
rc = -1;
}
}
return rc;
}


/*charcmp: return < 0 if s<t, 0 if s==t,>0 if s>t */
int charcmp(char *s, char *t)
{
char a, b;
int fold = (option & FOLD) ? 1 : 0;
int dir = (option & DIR) ? 1 : 0;
do {
if (dir) {
while (!isalnum(*s) && *s != ' ' && *s != '')
s++;
while (!isalnum(*t) && *t != ' ' && *t != '')
t++;
}
a = fold ? tolower(*s) : *s;
s++;
b = fold ? tolower(*t) : *t;
t++;
if (a == b && a =='')
return 0;
}while (a == b);
return a -b;
}


//readlines:read i/p lines
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] = '';
strcpy(p, line);
lineptr[nlines++] = p;
}
return nlines;
}

//writeline:write output lines
void writelines(char *lineptr[], int nlines)
{
int i;

for (i = 0; i < nlines; i++)
printf("%sn", lineptr[i]);
}


//cnumcmp:ompare p1 and p2 numerically
int numcmp(const void *p1, const void *p2)
{
char * const *s1 = reverse ? p2 : p1;
char * const *s2 = reverse ? p1 : p2;
double v1, v2;

v1 = atof(*s1);
v2 = atof(*s2);
if (v1 < v2)
return -1;
else if (v1 > v2)
return 1;
else
return 0;
}

/*qsort: sort v[left]....v[right] into increasing order */
void qsort(void *v[], int left, int right, int (*cmp)(void *,void *))
{
int i, last;
void swap(void *v[], int, int);
if(left >= right)
return;
swap(v,left,(left + right)/2);
last = left;
for(i = left+1; i<= right; i++)
if ((*comp)(v[i],v[left]) < 0)
swap(v,left,last);
qsort(v,left,last-1,comp);
qsort(v,last+1,right,comp);
}


void swap(void *v[], int i, int j)
{
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
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 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 generate sparse matrix.

C Program to generate sparse matrix.
A sparse matrix is a matrix that allows special techniques to take advantage of the large number of zero elements.Sparse matrix is very useful in engineering field, when solving the partial differentiation equations. Read more about how to generate sparse matrix.
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>
void main()
{
int A[10][10],B[10][3],m,n,s=0,i,j;
clrscr();
printf("nEnter the order m x n of the sparse matrixn");
scanf("%d%d",&m,&n);
printf("nEnter the elements in the sparse matrix(mostly zeroes)n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("n%d row and %d column: ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("The given matrix is:n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",A[i][j]);
}
printf("n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(A[i][j]!=0)
{
B[s][0]=A[i][j];
B[s][1]=i;
B[s][2]=j;
s++;
}
}
}
printf("nThe sparse matrix is given by");
printf("n");
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",B[i][j]);
}
printf("n");
}
getch();
}


Read more c programs 
C Basic
C Strings
Matrix in c

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 demonstrate sprintf statement.

C Program to demonstrate the ‘sprintf‘ statement. This example is a bit lame as the same effect can be seen with a ‘printf’. But, it does show a string being built and passed into a function. 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>

main()
{
int i=1; /* Define an integer variable. */
char message[80]; /* Text string */

/* format text and put into 'message' this a great
* improvement over using 'strcpy' and 'strcat' to
* build a text string.
*/
sprintf (message, "i is %i", i);
/* I may be stating the obvious but a '' is
* put on the end of the string. */

puts(message); /* Display message */

}

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

K and R C, Solution to Exercise 5-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.
C program to , Rewrite the routines of K & R C Programs Exercise 5-8 day_of_year and month_day with pointers instead of indexing.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>

static char daytab[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};



int day_of_year(int year, int month, int day)
{
int i, leap;
char *p;

leap = (year%4 == 0 && year%100 != 0) || year%400 == 0;

/* Set `p' to point at first month in the correct row. */
p = &daytab[leap][1];

/* Move `p' along the row, to each successive month. */
for (i = 1; i < month; i++) {
day += *p;
++p;
}
return day;
}

void month_day(int year, int yearday, int *pmonth, int *pday)
{
int i, leap;
char *p;

leap = (year%4 == 0 && year%100 != 0) || year%400 == 0;
p = &daytab[leap][1];
for (i = 1; yearday > *p; i++) {
yearday -= *p;
++p;
}
*pmonth = i;
*pday = yearday;
}


int main(void)
{
int year, month, day, yearday;

year = 2012;
month = 7;
day = 9;
printf("The date is: %d-%02d-%02dn", year, month, day);
printf("day_of_year: %dn", day_of_year(year, month, day));



yearday = 61; /* 2012-03-01 */
printf("Yearday is %dn", yearday);
month_day(year, yearday, &month, &day);
printf("month_day_pointer: %d %dn", month, day);

return 0;
}


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

K and R C, Solution to Exercise 5-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 to get next floating point analog of getint.The routine getfloat is similar to the routine getint. getfloat skips whitespaces, records the sign, and stores the integer part of the number at the address in fp.
getfloat also handles the practional part of the number(but not scientific notation). the fractional part is added to *fp in the same fashion as the integer part. 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<math.h>

int getfloat(float *fp)
{
int ch;
int sign;
int fraction;
int digits;

while (isspace(ch = getch())) /* skip white space */
;

if (!isdigit(ch) && ch != EOF && ch != '+'
&& ch != '-' && ch != '.') {
ungetch(ch);
return 0;
}

sign = (ch == '-') ? -1 : 1;
if (ch == '+' || ch == '-') {
ch = getch();
if (!isdigit(ch) && ch != '.') {
if (ch == EOF) {
return EOF;
} else {
ungetch(ch);
return 0;
}
}
}

*fp = 0;
fraction = 0;
digits = 0;
for ( ; isdigit(ch) || ch == '.' ; ch = getch()) {
if (ch == '.') {
fraction = 1;
} else {
if (!fraction) {
*fp = 10 * *fp + (ch - '0');
} else {
*fp = *fp + ((ch - '0') / pow(10, fraction));
fraction++;
}
digits++;
}
}

*fp *= sign;

if (ch == EOF) {
return EOF;
} else {
ungetch(ch);
return (digits) ? ch : 0;
}
}

//for testing... try the different one!
#include<stdio.h>

int main(void)
{
int ret;

do {
float f;

fputs("Enter a number: ", stdout);
fflush(stdout);
ret = getfloat(&f);
if (ret > 0) {
printf("You entered: %fn", f);
}
} while (ret > 0);

if (ret == EOF) {
puts("Stopped by EOF.");
} else {
puts("Stopped by bad input.");
}

return 0;
}



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

K and R C, Solution to Exercise 4-13:
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 reverse the string using the recursive methods.
In this program reverse determines the length of the string and then calls the reverser, which reverses the string s in place. 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>
/* reverse: reverse the string s in place */
void reverse(char s[])
{

void reverser(char s[], int i, int len);
reverser(s,0,strlen(s));
}

/* reverser: reverse string s in place recursive */
void reverser(char s[], int i, int len)
{
int c, j;
j = len - (i + 1);
if(i < j)
{
c = s[i];
s[i] = s[j];
s[j] = c;
reverser(s, ++i, len);
}
}

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

K and R C, Solution to Exercise 4-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 Program to modify the K & R C Programs Exercise 4-5, An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. revise the caluculater to use this aproach. 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<ctype.h>

#define MAXLINE 100
#define NUMBER '0' //SIGNAL THAT A NUMBER WAS FOUND
int getline(char line[], int limit);
int li = 0;
char line[MAXLINE];
/* Getop: get next operator or numeric operand. */
int Getop(char s[])
{
int i;
int c;

if(line[li] == '')
if(getline(line, MAXLINE) == 0)
return EOF;
else li = 0;
/* Skip whitespace */
while((s[0] = c = line[li++]) == ' ' || c == 't')
{
;
}
s[1] = '';

/* Not a number but may contain a unary minus. */
if(!isdigit(c) && c != '.' )
return c;
i = 0;

if(isdigit(c))
while(isdigit(s[++i] = c =line[i++]))
;

if(c == '.')
while(isdigit(s[++i] = c =line[i++]))
;
s[i] = '';
li--;
return NUMBER;
}

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 which produces its own source code as its output.

Write a c program which produces its own source code as its output.
This C program uses the C File i/o operations like fopen(), fclose(). In this program, We pritn the source code of the program.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>
int main()
{
FILE *fp;
char c;

fp = fopen(__FILE__,"r");

do{

c= getc(fp);

putchar(c);

}
while(c!=EOF);

fclose(fp);

return 0;

}


Read more Similar C Programs
C Basic

C File i/o

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 print ASCII values of all characters.

C Program to print ASCII values of all characters.
ASCII value is the American Standard Code for Information Interchange.
ASCII value is the numerical value, or order, of an character. There are 128 standard ASCII characters, numbered from 0 to 127. Extended ASCII adds another 128 values and goes to 255. The numbers are typically represented in decimal or in hexadecimal. 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>
int main(){

int i;
clrscr();
for(i=0;i<=255;i++)
printf("ASCII value of
character %c: %dn",i,i);
getch();
return 0;

}
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