Here's some simple programs I've written.
Code:
/* coffee */
#include <stdio.h>
#include <stdlib.h>
void total(int y) {
float x = (y * 2.99);
printf("Your total is $%0.2f.", x);
return;
}
int main(int argc, char *argv[]) {
int y;
if (!argv[1]) {
printf("How many cups of coffee would you like? ");
scanf("%d", &y);
total(y);
return 0;
}
y = (atoi(argv[1]));
total(y);
return 0;
}
Code:
/* digit counter */
#include <stdio.h>
int digits(const char *p) {
int x;
for (x = 0; *p != '\0'; p++) {
if (*p == '0' || *p == '1' || *p == '2' ||
*p == '3' || *p == '4' || *p == '5' ||
*p == '6' || *p == '7' || *p == '8' || *p == '9') x++;
}
return x;
}
int main() {
int x;
char y[256];
printf("Enter a string: ");
fgets(y, sizeof(y), stdin);
x = digits(y);
printf("That string has %d digits.\n", x);
return 0;
}
Code:
/* spaces counter */
#include <stdio.h>
int spaces(const char *p) {
int x;
for (x = 0; *p != '\0'; p++)
if (*p == ' ') x++;
return x;
}
int main() {
int x;
char y[256];
printf("Enter a string: ");
fgets(y, sizeof(y), stdin);
x = spaces(y);
printf("That string has %d spaces.\n", x);
return 0;
}
Code:
/* file reader */
#include <stdio.h>
int main(int argc, char *argv[]) {
if (!argv[1]) {
printf("USAGE: %s [input]\n", argv[0]);
return 1;
}
int x;
for (x = 1; x < argc; x++) {
FILE *in = (fopen(argv[x], "r"));
int y;
if (in == NULL) return 2;
while ((y = fgetc(in)) != EOF) putchar(y);
}
return 0;
}