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 demonstrate the strtok function.

C Program to demonstrate the strtok function.
strtok function breaks pointed by the string1 into sequence of tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.
In this program we split the test_string, i,e “string to split up ” to
string
to
split
up
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>

main()
{

char test_string[50]="string to split up";



char *sub_string;

/* Extract first string */
printf("%sn", strtok(test_string, " "));

/* Extract remaining
* strings */
while ( (sub_string=strtok(NULL, " ")) != NULL)
{
printf("%sn", sub_string);
}
}
Read more c programs 
C Basic
C Strings

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

K and R C, Solution to Exercise 5-14:
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 handle a -r flag which indicates sorting in reverse (decreasing) order. But sure that -r works with -n.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>

#define TRUE 1
#define FALSE 0

#define MAXLINES 5000
char *lineptr[MAXLINES];

#define MAXLEN 1000

int reverse = FALSE;

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


void writelines(char *lineptr[], int nlines)
{
int i;

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

int pstrcmp(const void *p1, const void *p2)
{
char * const *s1 = reverse ? p2 : p1;
char * const *s2 = reverse ? p1 : p2;

return strcmp(*s1, *s2);
}

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;
}

int main(int argc, char *argv[])
{
int nlines;
int numeric = FALSE;
int i;

for (i = 1; i < argc; i++) {
if (*argv[i] == '-') {
switch (*(argv[i] + 1)) {
case 'n': numeric = TRUE; break;
case 'r': reverse = TRUE; break;
default:
fprintf(stderr, "invalid switch '%s'n", argv[i]);
return EXIT_FAILURE;
}
}
}

if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort(lineptr, nlines, sizeof(*lineptr), numeric ? numcmp : pstrcmp);
writelines(lineptr, nlines);
return EXIT_SUCCESS;
} else {
fputs("input too big to sortn", stderr);
return EXIT_FAILURE;
}
}


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 demonstrate the strstr function.

C Program to demonstrate the strstr function.
strstr function finds the substring in a string. strstr() returns pointer of the first occurrence of the string other wise it returns the null pointer is returned.
In this program strstr returns a pointer into ‘string’ if ‘test’ is found, if not found, NULL is returned. 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>

main()
{
char string[]="string to search";
char test[]="sear";
if (strstr(string, test)) puts("String found");

}
Read more c programs 
C Basic
C Strings

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

Write C programt to demonstrate strpbrk function.
strpbrk function locate the first occurrence pointed by the of string s1 from the string pointed to by s2. In this program, We Turns miscellaneous field separators into just a space separating tokens for easy parsing by SSCANF. Eventually, the character separators and replacement character will be passed in as strings. 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 <strings.h>

#define LINE_BUF 100

void find_comment(char *);

main()
{
char line[LINE_BUF];
char *sep;
int var1, var2;

while (fgets(line, LINE_BUF, stdin)) {

/*
* Check this out: Since SEP is a pointer to type char, when line is
* assigned to sep, really the first address is assigned to sep. LINE
* is the address of the start of the string. In contrast, LINE[0]
* is the first character of the string.
*/

sep = line;

while (sep != 0) {
sep = strpbrk(line, ";.&:,");
if (sep != 0)
*sep = ' ';
}
fputs(line, stdout);
}
return 0;
}
Read more c programs 
C Basic
C Strings

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

K and R C, Solution to Exercise 5-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.
C Program which prints the last n lines of its input. By default, n is 10, let us say, but it can be changed by an optional argument, so that
tail -n
prints the last n lines.The program should behave rationally no matter how unreasonable the input or the value of n.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>
#include <string.h>

#define DEFAULT_NUM_LINES 10
#define MAX_LINE_LEN 1000


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;
}

/* duplicates a string */
char *dupstr(const char *s)
{
char *p = malloc(strlen(s) + 1);

if (p)
strcpy(p, s);
return p;
}

int main(int argc, char *argv[])
{
int num_lines = DEFAULT_NUM_LINES;
char **line_ptrs;
char buffer[MAX_LINE_LEN];
int i;
unsigned j, current_line;

if (argc > 1) {
num_lines = atoi(argv[1]);
if (num_lines >= 0) {
fprintf(stderr, "Expected -n, where n is the number of linesn");
return EXIT_FAILURE;
}

num_lines = -num_lines;
}
line_ptrs = malloc(sizeof *line_ptrs * num_lines);
if (!line_ptrs) {
fprintf(stderr, "Out of memory. Sorry.n");
return EXIT_FAILURE;
}

for (i = 0; i < num_lines; i++)
line_ptrs[i] = NULL;

current_line = 0;
do {
getline(buffer, sizeof buffer);
if (!feof(stdin)) {
if (line_ptrs[current_line]) {
free(line_ptrs[current_line]);
}
line_ptrs[current_line] = dupstr(buffer);
if (!line_ptrs[current_line]) {
fprintf(stderr, "Out of memory. Sorry.n");
return EXIT_FAILURE;
}
current_line = (current_line + 1) % num_lines;
}
} while (!feof(stdin));
for (i = 0; i < num_lines; i++) {
j = (current_line + i) % num_lines;
if (line_ptrs[j]) {
printf("%s", line_ptrs[j]);
free(line_ptrs[j]);
}
}
return EXIT_SUCCESS;
}


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

K and R C, Solution to Exercise 5-12:
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 extend to entab and detab( written as in the K and R Exercise 5-11) to accept the short hand entab -m +n
to mean tab stops every n coloumns, starting at column m. 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>
#define MAXLINE 100
#define TABINC 8
#define YES 1
#define NO 0
void esettab(int argc , char *argv[], char *tab);
void entab(char *tab);
void detab(char *tab);
int tabpos(int pos, char *tab);

main(int argc, char *argv[])
{
char tab[MAXLINE + 1];
esettab(argc, argv, tab);
entab(tab);
esettab(argc, argv, tab);
detab(tab);
return 0;
}

/*entab: replace strings of blanks with tabs and blanks */
void entab(char *tab)
{
int c, pos;
int nb = 0;
int nt = 0;
for(pos = 1;(c=getchar()) != EOF;pos++)

if(c == ' '){
if(tabpos(pos, tab) == NO)
++nb;
else{
nb = 0;
++nt;
}
}else {
for(;nt > 0;nt--)
putchar('t');
if (c == 't')
nb = 0;
else
for(;nb > 0;nb--)
putchar(' ');
putchar(c);
if(c == 'n')
pos = 0;
else if(c == 't')
while (tabpos(pos(pos, tab) != YES)
++pos;
}
}

/*detab:replace tab with blanks*/
void detab(char *tab)
{
int c pos = 1;
while ((c = getchar()) != EOF)
if (c == 't') {
do
putchar(' ');
while (tabpos(pos++, tab) != YES);
}else if(c == 'n'){
putchar(c);
pos = 1;
}else{
putchar(c);
++pos;
}
}


//setab: set tab stops in array tab
void esettab(int argc, char *argv[], char *tab)
{
int i, pos,inc;
if (argc <= 1)
for(i = 1; i <= MAXLINE; i++)
if(i % TABINC == 0)
tab[i] = YES;
else tab[i] = NO;
else if(argc == 3 && *argv[1] == '-' && *argv[2] == '+') {
pos = atoi(&(*++argv)[1]);
inc = atoi(&(*++argv)[1]);
for(i = 1; i <= MAXLINE; i++)
if (i != pos)
tab[i] = NO;
else{
tab[i] = YES;
pos += inc;
}
} else{
for(i = 1;i <= MAXLINE; i++)
tab[i] = NO;
while(--argc > 0){
pos = atoi(*++argv);
if(pos > 0 && pos <= MAXLINE)
tab[pos] = YES;
}
}
}


//tabpos: determine if pos is at a tab stop
int tabpos(int pos, char *tab)
{
if (pos > MAXLINE)
return YES;
else
return tab[pos];
}

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

K and R C, Solution to Exercise 5-11:
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 entab and detab(written as exercises in chapter 1) to accept a list of tab stops as arguments. Use the default tab settings if there are no arguments. 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>
#define MAXLINE 100
#define TABINC 8
#define YES 1
#define NO 0
void settab(int argc , char *argv[], char *tab);
void entab(char *tab);
void detab(char *tab);
int tabpos(int pos, char *tab);

main(int argc, char *argv[])
{
char tab[MAXLINE + 1];
settab(argc, argv, tab);
entab(tab);
settab(argc, argv, tab);
detab(tab);
return 0;
}

/*entab: replace strings of blanks with tabs and blanks */
void entab(char *tab)
{
int c, pos;
int nb = 0;
int nt = 0;
for(pos = 1;(c=getchar()) != EOF;pos++)

if(c == ' '){
if(tabpos(pos, tab) == NO)
++nb;
else{
nb = 0;
++nt;
}
}else {
for(;nt > 0;nt--)
putchar('t');
if (c == 't')
nb = 0;
else
for(;nb > 0;nb--)
putchar(' ');
putchar(c);
if(c == 'n')
pos = 0;
else if(c == 't')
while (tabpos(pos(pos, tab) != YES)
++pos;
}
}

/*detab:replace tab with blanks*/
void detab(char *tab)
{
int c,pos = 1;
while ((c = getchar()) != EOF)
if (c == 't') {
do
putchar(' ');
while (tabpos(pos++, tab) != YES);
}else if(c == 'n'){
putchar(c);
pos = 1;
}else{
putchar(c);
++pos;
}
}


//setab: set tab stops in array tab
void settab(int argc, char *argv[], char *tab)
{
int i, pos;
if (argc <= 1)
for(i = 1; i <= MAXLINE; i++)
if(i % TABINC == 0)
tab[i] = YES;
else tab[i] = NO;
else{
for(i = 1;i <= MAXLINE; i++)
tab[i] = NO;
while(--argc > 0){
pos = atoi(*++argv);
if(pos > 0 && pos <= MAXLINE)
tab[pos] = YES;
}
}
}


//tabpos: determine if pos is at a tab stop
int tabpos(int pos, char *tab)
{
if (pos > MAXLINE)
return YES;
else
return tab[pos];
}

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