HELLO WORLD!
PRINT CELSIUS TO FAHRENHEIT:
#include <stdio.h>
int main(){
printf("hello, world\n");
return 0;
}
PRINT CELSIUS TO FAHRENHEIT WITH FOR:
#include <stdio.h>
/* print Celsius-Farenheit table
for celsius - 0, 20, ..., 300 */
int main(){
printf("print Celsius-Farenheit table\n");
float fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
celsius = lower;
while(celsius <= upper){
fahr = (9 * celsius + 160 )/ 5.0;
printf("%3.0f\t%6.2f\n", celsius, fahr);
celsius = celsius + step;
}
}
ADDING SYMBOLIC CONSTANTS:
#include <stdio.h>
/* print Celsius-Farenheit table
for fahr - 0, 20, ..., 300 */
int main(){
printf("print Celsius-Farenheit table\n");
float celsius;
for(celsius = 0; celsius <= 300; celsius += 20)
printf("%3.0f\t%6.2f\n", celsius, (9 * celsius + 160 )/ 5.0);
}
COPY INPUT TO OUTPUT 1ST VERSION
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
/* print Celsius-Farenheit table
for fahr - 0, 20, ..., 300 */
int main(){
printf("print Celsius-Farenheit table\n");
float celsius;
for(celsius = LOWER; celsius <= UPPER; celsius += STEP)
printf("%3.0f\t%6.2f\n", celsius, (9 * celsius + 160 )/ 5.0);
}
COPY INPUT TO OUTPUT 2ND VERSION
#include <stdio.h>
/* copy input to output 1st version */
int main(){
char c;
c = getchar();
while(c != EOF){
putchar(c);
c = getchar();
}
}
COUNT CHARACTERS IN INPUT 1ST VERSION
#include <stdio.h>
/* copy input to output 2nd version */
int main(){
char c;
while((c = getchar()) != EOF){
putchar(c);
}
}
COUNT CHARACTERS IN INPUT 2ND VERSION
#include <stdio.h>
/* count characters in input 1st version */
int main(){
long nc = 0;
while(getchar() != EOF){
nc++;
}
printf("\n%ld\n", nc);
}
COUNT LINES IN INPUT
#include <stdio.h>
/* count characters in input 2nd version */
int main(){
double nc = 0;
for(nc = 0; getchar() != EOF; nc++)
;
printf("\n%.0f\n", nc);
}
A PROGRAM TO COUNT TABS, BLANKS AND NEWLINES
#include <stdio.h>
/* count lines in input */
int main(){
int nl = 0, c;
while((c = getchar()) != EOF){
if (c == '\n')
nl++;
}
printf("%d\n", nl);
}
COUNT LINES WORDS AND CHARACTERS
#include <stdio.h>
/* count blanks, newlines and tabs */
int main(){
int blanks = 0, newlines = 0, tabs = 0, c;
while((c = getchar()) != EOF){
if (c == ' ')
blanks += 1;
else if(c == '\n')
newlines += 1;
else if(c == '\t')
tabs += 1;
}
printf("\n%d\t%d\t%d\n", blanks, newlines, tabs);
}
PRINT ONE WORD PER LINE
#include <stdio.h>
/* count lines, words and characters in input */
#define IN 1
#define OUT 0
int main(){
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while((c = getchar()) != EOF){
++nc;
if(c == '\n'){
if(nl == 0)
nl = 2;
else
nl++;
}
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if(state == OUT){
state = IN;
nw++;
}
}
printf("\n %d %d %d \n", nc, nw, nl);
}
COUNT DIGITS, WHITE SPACE AND OTHER
#include <stdio.h>
/* print one word per line */
int main(){
int c;
while((c = getchar()) != EOF){
if(c == ' ' || c == '\n' || c == '\t'){
c = '\n';
}
printf("%c", c);
}
}
SHOW HISTOGRAM OF THE LENGTHS OF WORDS
#include <stdio.h>
/* count digits, white space, others */
int main(){
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for(i = 0; i < 10; i++)
ndigit[i] = 0;
while((c = getchar()) != EOF){
if(c >= '0' && c <= '9')
ndigit[c - '0'] ++;
else if(c == ' ' || c == '\t' || c == '\n')
nwhite++;
else
nother++;
}
printf("digits= ");
for(i = 0; i < 10; i++)
printf("%d ", ndigit[i]);
printf(", white space = %d, other = %d", nwhite, nother);
}
SHOW A HISTOGRAM OF FREQUENCIES OF DIFFERENT
CHARACTERS IN ITS INPUT
#include <stdio.h>
/* Show a histogram of the lengths of words in its input */
#define IN 1
#define OUT 0
int main(){
int nc = 0, c, state;
state = OUT;
while((c = getchar()) != EOF){
if(c == ' ' || c == '\n' || c == '\t'){
state = OUT;
for(int i = 0; i < nc; i++)
printf("%c", '-');
printf("%c", '\n');
}else if(state == OUT){
state = IN;
nc = 1;
}else{
nc += 1;
}
}
}
POWER FUNCTION
#include <stdio.h>
/* Show a histogram to print the frequencies of different characters in its input */
int main(){
int fq[1005], c;
for(int i = 0; i < 130; i++)
fq[i] = 0;
while((c = getchar()) != EOF){
fq[c]++;
}
printf("%c", '\n');
for(int i = 0; i < 130; i++){
if(fq[i] > 0){
printf("frequency of %c = ", i);
for(int j = 0; j < fq[i]; j++)
printf("%c", '-');
printf("%c", '\n');
}
}
}
CONVERT FAHRENHEIT TO CELSIUS WITH A FUNCTION
#include <stdio.h>
int power(int n, int m);
int main(){
int i;
for(i = 0; i < 10; i++)
printf("%d %d %d \n", i, power(2, i), power(-3, i));
}
/* power raise base to n-th power */
int power(int base, int n){
int i, p;
p = 1;
for(i = 1; i <= n; i++)
p *= base;
return p;
}
PRINT LONGEST INPUT LINE
#include <stdio.h>
/* print Celsius-Farenheit table
for celsius - 0, 20, ..., 300 with a function */
#define LOWER 0
#define UPPER 300
#define STEP 20
int conversion(int fahr){
int celsius = (5 * (fahr - 32) / 9);
return celsius;
}
int main(){
for(int i = LOWER; i <= UPPER; i += STEP)
printf("%d %d \n", i, conversion(i));
}
IN CASE THAT THE LINE IS GREATER THAN THE
MAXIMUM
#include <stdio.h>
#define MAXLINE 5
int getLine(char line[], int maxline);
void copy(char to[], char from[]);
/* print longest input line */
int main(){
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while( (len = getLine(line, MAXLINE)) > 0){
if(len > max){
max = len;
copy(longest, line);
}
}
if(max > 0){
printf("%s\n", longest);
}
return 0;
}
int getLine(char s[], int lim){
int c, i;
for(i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; i++){
s[i] = c;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[]){
int i = 0;
while((to[i] = from[i]) != '\0')
i++;
}
PRINT ALL LINES GREATER THAN 80 CHARACTERS
#include <stdio.h>
#define MAXLINE 1000
int get_line(char line[]);
void copy(char from[], char to[]);
int main(){
int len, maxLen = 0;
char line[MAXLINE];
char maxLine[MAXLINE];
while((len = get_line(line)) > 0){
if(maxLen < len){
maxLen = len;
copy(line , maxLine);
}
printf("line_length: %d\n", len);
}
printf("%s\n", maxLine);
return 0;
}
int get_line(char line[]){
int c, i;
for(i = 0; i < MAXLINE && (c = getchar()) != EOF && c != '\n'; i++)
line[i] = c;
if (c == '\n'){
line[i] = c;
++i;
}
line[i] = '\0';
/* In case that the line is longer than the maximum */
while(c != EOF && c != '\n'){
i++;
c = getchar();
}
return i;
}
void copy(char from[], char to[]){
int i = 0;
while((to[i] = from[i]) != '\0')
i++;
}
REMOVING TRAILING BLANKS, TABS
#include <stdio.h>
#define MAXLINE 1000
int get_line(char line[]);
int main(){
int len, maxLen = 0;
char line[MAXLINE];
while((len = get_line(line)) > 0){
if(len > 80){
printf("line = %s", line);
}
}
return 0;
}
int get_line(char line[]){
int c, i;
for(i = 0; i < MAXLINE && (c = getchar()) != EOF && c != '\n'; i++)
line[i] = c;
if (c == '\n'){
line[i] = c;
++i;
}
line[i] = '\0';
/* In case that the line is longer than the maximum */
while(c != EOF && c != '\n'){
i++;
c = getchar();
}
return i;
}
WRITE A FUNCTION reverse(s) THAT REVERSES THE
STRING s
#include <stdio.h>
#define MAXLINE 1000
int get_line(char line[]);
int main(){
int len, maxLen = 0;
char line[MAXLINE];
while((len = get_line(line)) > 0){
printf("line = %slength = %d\n", line, len);
}
return 0;
}
int get_line(char line[]){
int c, i;
for(i = 0; i < MAXLINE && (c = getchar()) != EOF && c != '\n'; i++)
line[i] = c;
if (c == '\n'){
i--;
while(line[i] == ' ' || line[i] == '\t')
i--;
i++;
line[i] = c;
++i;
}
line[i] = '\0';
/* In case that the line is longer than the maximum */
while(c != EOF && c != '\n'){
i++;
c = getchar();
}
return i;
}
LONGEST LINE WITH EXTERN VARIABLES
#include <stdio.h>
#define MAXLINE 1000
int get_line(char line[]);
void reverse(char s[], int len);
int main(){
int len, maxLen = 0;
char line[MAXLINE];
while((len = get_line(line)) > 0){
reverse(line, len);
printf("%s\n", line);
}
return 0;
}
int get_line(char line[]){
int c, i;
for(i = 0; i < MAXLINE && (c = getchar()) != EOF && c != '\n'; i++)
line[i] = c;
if (c == '\n'){
line[i] = c;
++i;
}
line[i] = '\0';
/* In case that the line is longer than the maximum */
while(c != EOF && c != '\n'){
i++;
c = getchar();
}
return i;
}
void reverse(char s[], int len){
int i = 0;
for(i = 0; i < (len - 1) / 2; i++){
char aux = s[i];
s[i] = s[len - i - 2];
s[len - i - 2] = aux;
}
}
#include <stdio.h>
#define MAXLINE 1000
int maximum;
char line[MAXLINE];
char longest[MAXLINE];
int get_line(void);
void copy(void);
int main(){
int len;
extern int maximum;
extern char longest[];
maximum = 0;
while((len = get_line()) > 0){
if(len > maximum){
maximum = len;
copy();
}
printf("%d\n", maximum);
}
if(maximum > 0){
printf("%s", longest);
}
return 0;
}
int get_line(void){
int c, i;
extern char line[];
for(i = 0; i < MAXLINE - 1 && (c = getchar()) != EOF && c != '\n'; i++)
line[i] = c;
if(c == '\n'){
line[i] = c;
i++;
}
return i;
}
void copy(void){
int i;
extern char line[], longest[];
i = 0;
while((longest[i] = line[i]) != '\0')
i++;
}