L-17 MCS 260 Fri 28 Sep 2001
Below are the complete versions of the programs we discussed in class.
/* L-17 MCS 260 Fri 28 Sep 2001 : printing the ASCII character table.
ASCII = American Standard Code for Information Interchange
Every character is represented by a byte = 8 bits, thus there
are 2^8 = 256 possible values to represent characters.
The program below displays the ASCII codes on screen. */
#include<stdio.h>
int main(void)
{
int i;
printf("The ASCII codes : ");
for (i = 0; i < 256; i++)
{
printf("%4c", i);
if (i % 10 == 0) printf("\n");
}
printf("\n");
return 0;
}
From the ASCII table we observe some special characters :
/* L-17 MCS 260 Fri 28 Sep 2001 : some special characters.
We have already frequently used the newline symbol \n.
The program below illustrates uses of other special symbols,
like the backslash to print quotes and backslashes.
With alert we can wake up the user when asking for input. */
#include<stdio.h>
int main(void)
{
char ans;
printf("Here we print some special characters : \n");
printf("Are your ready ?\a (y/n) ");
scanf("%c", &ans);
if (ans != 'y')
printf("Sorry for disturbing...\n");
else
{
printf(" \aThe newline symbol is \"\\n\".\n");
printf("\a");
printf(" For alert, use \"\\a\".\n");
}
return 0;
}
An application is to replace tabs by spaces :
/* L-17 MCS 260 Fri 28 Sep 2001 : replace every tab by a single space
The program below replaces every tab in the input by a single space.
To run the program, we bet put all the input first in a separate file,
for example, the file "input" can consist of numbers separated by tabs:
1 2 3 5 6
6 8
Then the output looks like
1 2 3 5 6
6 8
We can run the program like
a.out < input > output
which takes the file "input" and creates the reformatted file "output". */
#include<stdio.h>
int main(void)
{
int c; /* char c; equivalent code */
while ((c = getchar()) != EOF) /* while (scanf("%c",&c) > 0) */
if (c == '\t')
putchar(' '); /* printf(" "); */
else
putchar(c); /* printf("%c", c); */
return 0;
}