void swap(void *v[], int i, int j){
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
numcmp:compare strings numerically
#include <stdlib.h>
/*numcmp: compare s1 and s2 numerically */
int numcmp(char *s1, char *s2){
double v1, v2;
v1 = atof(s1);
v2 = atof(s2);
if (v1 < v2)
return -1;
else if (v1 > v2)
return 1;
else
return 0;
}
qsort: version 2
/*qsort: sort v[left] ... v[right] into increasing order */
void qsort(void *v[], int left, int right,
int (*comp)(void *, void *)){
int i, last;
void swap(void *v[], int ,int);
if (left >= right) /*do nothing if array contains */
return; /* fewer than two elements */
swap(v, left, (left + right)/2);
last = left;
for (i = left + 1; i<= right;i++)
if ((*comp)(v[i], v[left]) < 0)
swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last - 1, comp);
qsort(v, last + 1, right, comp);
}
sort input lines lexicographically or numerically.
#include <stdio.h>
#include <string.h>
#define MAXLINES 5000 /*max #lines to be sorted */
char *lineptr[MAXLINES]; /*pointers to text lines */
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *));
int numcmp(char *, char *);
/*sort input lines */
main(int argc, char *argv[]){
int nlines; /* number of input lines read */
int numeric = 0; /*1 if numeric sort */
if (argc > 1 && strcmp(argv[1], "-n") == 0)
numeric = 1;
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort((void **) lineptr, 0, nlines -1,
(int (*) (void*, void*))(numeric ? numcmp : strcmp));
writelines(lineptr, nlines);
return 0;
}else {
printf("input too big to sort\n");
return 1;
}
}
pattern-finding program,similar to grep.
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
int getline(char *line, int max);
/*find: print lines that match pattern from 1st arg */
main(int argc, char *argv[]){
char line[MAXLINE];
long lineno = 0;
int c, except = 0, number = 0, found = 0;
while (--argc > 0 && (*++argv[0] == '-')
while(c == *++argv[0])
switch(c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find; illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
if (argc != 1)
printf("Usage; find -x -n pattern\n");
else
while (getline(line, MAXLINE) > 0){
lineno++;
if ((strstr(line, *argv) != NULL) != except){
if (number)
printf("%ld: ", lineone);
printf("%s", line):
found++;
}
}
return found;
}
/*strstr(s, t) returns a pointer to the first occurrence of the string t in the string s, or NULL if there is none. */
echo command line arguments:version 3
#include <stdio.h>
/*echo command-line arguments; 2nd version */
main(int argc, char *argv[]){
while (--argc > 0)
printf((argc > 1) ? "%s" : "%s", *++argv);
printf("\n");
return 0;
}
echo command line arguments:version 2.
#include <stdio.h>
/*echo command-line arguments; 2nd version */
main(int argc, char *argv[]){
while(--argc > 0)
printf("%s%s", *++argv, (argc > 1) ? " " : "");
printf("\n");
return 0;
}
echo command line arguments
#include <stdio.h>
/*echo command-line arguments:1st version */
main(int argc, char *argv[]){
int i;
for(i =1; i <argc;i++)
printf("%s%s", argv[i], (i< argc -1)? " ": "");
printf("\n");
return 0;
}
sort a set of textlines into alphabetic order.
#include <stdio.h>
#include <string.h>
#define MAXLINES 5000 /* max #lines to be sorted */
char *lineptr[MAXLINES]; /* pointers to text lines */
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(char *lineptr[], int left, int right);
/* sort input lines */
main(){
int nlines; /* number of input lines read */
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort(lineptr, 0, nlines -1);
writelines(lineptr, nlines);
return 0;
} else {
printf("error: input too big to sort\n");
return 1;
}
}
String compare
/* strcmp: return <0 if s<t, 0 if s==t, >0 if s>t */
int strcmp(char *s, char *t){
int i;
for (i = 0; s[i] == t[i]; i++)
if (s[i] == '\0')
return 0;
return s[i] - t[i];
}
/*strcmp,pointer version: return < 0 if s<t, 0 if s == t, >0 if s>t */
int strcmp(char *s, char *t){
for (: *s == *t; s++, t++)
if (*s == '\0')
return 0;
return *s - *t;
}