Finished exercise 3-2. Starting 3.5

This commit is contained in:
kdeng00
2020-10-20 23:48:46 -04:00
parent 4ca75f9f09
commit bd1f02ec5e
2 changed files with 194 additions and 0 deletions
+119
View File
@@ -0,0 +1,119 @@
/*
*
* Exercise 3-2. Write a function escape(s,t) that converts characters like
* newline and tab into visible escape sequences like '\n' and '\t' as it
* copies the string t to s. Use a switch. Write a function for the other
* direction as well, converting escape sequences into real characters.
*
* Author: Kun Deng
*/
#include <stdio.h>
#define ARRAY_SIZE 10
#define BUFFER_SIZE (ARRAY_SIZE * 2)
// Copies string t to s but the escape sequences
// will be visible
void escape(char *s, char *t);
// Copies string t to s but the escape sequences
// will be their real characters
void ensnare(char *s, char *t);
int main()
{
printf("Enter characters:\n");
char unproccessed[ARRAY_SIZE];
char escaped[BUFFER_SIZE];
char ensared[ARRAY_SIZE];
char c;
int i;
for (i = 0; i != ARRAY_SIZE; ++i)
{
c = getchar();
if (c == EOF)
{
--i;
continue;
}
unproccessed[i] = c;
}
unproccessed[i] = '\0';
printf("\n\nUnproccessed string: %s\n", unproccessed);
escape(escaped, unproccessed);
printf("\n\nEscaped sequences replaced string: %s\n", escaped);
ensnare(ensared, escaped);
printf("\n\nEscaped sequences returned string: %s\n", ensared);
return 0;
}
void escape(char *s, char *t)
{
int j = 0;
char c;
for (int i = 0; ((c = t[i])) != '\0'; ++i, ++j)
{
switch (c)
{
case '\n':
s[j++] = '\\';
s[j] = 'n';
break;
case '\t':
s[j++] = '\\';
s[j] = 't';
break;
default:
s[j] = c;
break;
}
}
s[j] = '\0';
}
void ensnare(char *s, char *t)
{
int j = 0;
// If one that means the previous character was the beginning of an escape
// sequence
int escape_found = 0;
char c;
char rr[] = "thecarinspace\0";
for (int i = 0; ((c = t[i])) != '\0'; ++i, ++j)
{
switch (c)
{
case '\\':
escape_found = 1;
--j;
break;
case 'n':
s[j] = (escape_found == 1) ? 10 : c;
escape_found = 0;
break;
case 't':
s[j] = (escape_found == 1) ? 9 : c;
escape_found = 0;
break;
default:
s[j] = c;
break;
}
}
s[j] = '\0';
}
+75
View File
@@ -0,0 +1,75 @@
/*
*
* The program demonstrates the use of the switch statement while counting
* the occurrences of digits, white spaces (including actual white space,
* new line, and a tab character), and other types of characters
*
* Author: Kun Deng
*/
#include <stdio.h>
#define ARRAY_SIZE 13
int main()
{
int c, white_space_count, other_character_count;
int digit_count[ARRAY_SIZE];
white_space_count = other_character_count = 0;
for (int i = 0; i < ARRAY_SIZE; ++i)
{
digit_count[i] = 0;
}
while ((c = getchar()) != EOF)
{
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
// If no break statement is provided in a case then
// the code can continue to execute other cases where
// the expression matches the case.
case '9':
++digit_count[c - '0'];
// Break statements causes the control flow of the program
// to exit out of the block of code. In this case the
// break statement will cause the control flow to exit
// from the switch statement
break;
case ' ':
case '\n':
case '\t':
++white_space_count;
break;
default:
// Good practice to have a default case and have a break statement.
// Just in case more cases are added to the future
++other_character_count;
break;
}
}
printf("Digits =");
for (int i = 0; i < ARRAY_SIZE; ++i)
{
printf(" %d", digit_count[i]);
}
printf(", white space = %d, other = %d\n",
white_space_count, other_character_count);
return 0;
}