Backspaces and terminals. Left off on 1.5.4 Word Counting
This commit is contained in:
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Exercise 1-8. Write a program to count blanks, tabs, and new lines.
|
||||||
|
*
|
||||||
|
* Author: Kun Deng
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define BLANK_SPACE 32
|
||||||
|
#define TAB 9
|
||||||
|
#define NEW_LINE 10
|
||||||
|
#define EXIT_CONDITION 50 // ASCII value of 50 is the numerical 2 value
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int blank_count = 0;
|
||||||
|
int tab_count = 0;
|
||||||
|
int new_line_count = 0;
|
||||||
|
|
||||||
|
printf ("Enter 2 to print the results of how many blanks, tabs, and new lines were entered\n");
|
||||||
|
|
||||||
|
for (int c = getchar(); c != EOF; c = getchar())
|
||||||
|
{
|
||||||
|
if (c == BLANK_SPACE)
|
||||||
|
{
|
||||||
|
++blank_count;
|
||||||
|
}
|
||||||
|
else if (c == TAB)
|
||||||
|
{
|
||||||
|
++tab_count;
|
||||||
|
}
|
||||||
|
else if (c == NEW_LINE)
|
||||||
|
{
|
||||||
|
++new_line_count;
|
||||||
|
}
|
||||||
|
else if (c == EXIT_CONDITION)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("ASCII value of %d is character %c\n", c, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("blank count: %d\n", blank_count);
|
||||||
|
printf("tab count: %d\n", tab_count);
|
||||||
|
printf("new line count: %d\n", new_line_count);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Exercise 1-9. Write a program to copy its input to its output, replace each string of one or more blanks
|
||||||
|
* by a single blank
|
||||||
|
*
|
||||||
|
* Author: Kun Deng
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define BLANK_SPACE 32
|
||||||
|
#define EXIT_CONDITION 50 // ASCII value of 50 is the numerical 2 value
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int blank_count = 0;
|
||||||
|
int other = 0;
|
||||||
|
|
||||||
|
printf ("Enter 2 to escape\n");
|
||||||
|
printf ("Enter a string that contains multiple spaces in a row. ");
|
||||||
|
printf("The output will replace those spaces with a single space\n");
|
||||||
|
|
||||||
|
int previous_char = 0;
|
||||||
|
|
||||||
|
for (int c = getchar(); c != EOF; c = getchar())
|
||||||
|
{
|
||||||
|
if (c == EXIT_CONDITION)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((c == BLANK_SPACE && c != previous_char) || c != BLANK_SPACE)
|
||||||
|
{
|
||||||
|
putchar(c);
|
||||||
|
|
||||||
|
if (c == BLANK_SPACE)
|
||||||
|
{
|
||||||
|
++blank_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
++other;
|
||||||
|
}
|
||||||
|
|
||||||
|
previous_char = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("blank count: %d\n", blank_count);
|
||||||
|
printf("other count: %d\n", other);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
*
|
||||||
|
* Exercise 1-10. Write a program to copy its input to its output, replacing each tab by \t,
|
||||||
|
* each backspace by \b, and each backslash by \\. This makes tabs and backspaces visible
|
||||||
|
* in an unambiguous way.
|
||||||
|
*
|
||||||
|
* Author: Kun Deng
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define EXIT_CONDITION 50 // ASCII value of 50 is the numerical 2 value
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int blank_count = 0;
|
||||||
|
int tab_count = 0;
|
||||||
|
int new_line_count = 0;
|
||||||
|
|
||||||
|
printf("Each instance of a tab is replaced by \\t\n");
|
||||||
|
printf("Each instance of backspace is replaced by \\b\n");
|
||||||
|
printf("Each instance of backslash is replaced by \\\\\n");
|
||||||
|
printf("Enter 2 to escape\n");
|
||||||
|
|
||||||
|
int c = 0;
|
||||||
|
|
||||||
|
// Ctrl-H represents the backspace character
|
||||||
|
while ((c = getchar()) != EOF)
|
||||||
|
{
|
||||||
|
if (c == '\\')
|
||||||
|
{
|
||||||
|
printf("\\\\");
|
||||||
|
}
|
||||||
|
else if (c == '\t')
|
||||||
|
{
|
||||||
|
printf("\\t");
|
||||||
|
}
|
||||||
|
else if (c == '\b')
|
||||||
|
{
|
||||||
|
printf("\\b");
|
||||||
|
}
|
||||||
|
else if (c == EXIT_CONDITION)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user