Zu 13)
Code: Alles auswählen
#include <stdio.h>
int
main(int argc, char *argv[])
{
char *esc;
int litdefault;
int escaped;
int c;
if (argc != 3) {
fprintf(stderr, "Usage: %s ESCAPE {M|L}\n", argv[0]);
return 1;
}
esc = argv[1];
if (esc[1] != '\0') {
fprintf(stderr, "Escape character must be a single character\n");
return 2;
}
litdefault = *argv[2] == 'L';
while ((c=getchar()) != EOF) {
if (c == *esc) {
printf("%s%c", esc, c);
} else {
printf("%s%c", (litdefault ? "" : esc), c);
}
}
putchar('\n');
return 0;
}
Code: Alles auswählen
#include <stdio.h>
int
main(int argc, char *argv[])
{
char esc;
int litdefault;
int escaped;
int c;
if (argc != 3) {
fprintf(stderr, "Usage: %s ESCAPE {M|L}\n", argv[0]);
return 1;
}
esc = *argv[1];
litdefault = *argv[2] == 'L';
while ((c=getchar()) != EOF) {
if (escaped) {
if (c == esc) {
printf("Literal %c\n", c);
escaped = 0;
} else {
printf("%s %c\n", (litdefault ? "Meta" : "Literal"), c);
escaped = 0;
}
} else {
if (c == esc) {
escaped = 1;
} else {
printf("%s %c\n", (litdefault ? "Literal" : "Meta"), c);
}
}
}
return 0;
}
Aufrufbeispiele fuer die Aufgaben 4 und 8:
Code: Alles auswählen
:-/ printf %s Haus | ./02-13 a L
Haaus
:-/ printf %s A,BCD,E,FG,,H | ./02-14 , L
Literal A
Meta B
Literal C
Literal D
Meta E
Meta F
Literal G
Literal ,
Literal H