Write a C program to print escape sequence.
#include <stdio.h>
#include <string.h>
int main() {
char input[8];
char escape[9][4] = {"\\a", "\\b", "\\f", "\\n",
"\\r", "\\t", "\\", "\\'", "\\\""};
int i;
/* provide escape characters to the user to select */
printf("\\a – Bell (beep)\n");
printf("\\b – Backspace\n");
printf("\\f – Formfeed\n");
printf("\\n – New line\n");
printf("\\r – Carriage return\n");
printf("\\t – Horizontal tab\n");
printf("\\ – Backslash\n");
printf("\\' – Single quotation mark\n");
printf("\\\" – Double quotation mark");
printf("\nEnter your input escape sequence:");
/* get the input escape character */
fgets(input, 8, stdin);
input[strlen(input) - 1] = '\0';
/*
* find the index of the given escapce
* sequence in escape array
*/
for (i = 0; i < 9; i++) {
if (strcmp(input, escape[i]) == 0) {
break;
}
}
#include <string.h>
int main() {
char input[8];
char escape[9][4] = {"\\a", "\\b", "\\f", "\\n",
"\\r", "\\t", "\\", "\\'", "\\\""};
int i;
/* provide escape characters to the user to select */
printf("\\a – Bell (beep)\n");
printf("\\b – Backspace\n");
printf("\\f – Formfeed\n");
printf("\\n – New line\n");
printf("\\r – Carriage return\n");
printf("\\t – Horizontal tab\n");
printf("\\ – Backslash\n");
printf("\\' – Single quotation mark\n");
printf("\\\" – Double quotation mark");
printf("\nEnter your input escape sequence:");
/* get the input escape character */
fgets(input, 8, stdin);
input[strlen(input) - 1] = '\0';
/*
* find the index of the given escapce
* sequence in escape array
*/
for (i = 0; i < 9; i++) {
if (strcmp(input, escape[i]) == 0) {
break;
}
}
/* print the given escape sequence */
switch(i) {
case 0:
printf("Bell - \\a\n");
break;
case 1:
printf("Backspace - \\b\n");
break;
case 2:
printf("Formfeed - \\f\n");
break;
case 3:
printf("Newline - \\n\n");
break;
case 4:
printf("Carriage return - \\r\n");
break;
case 5:
printf("Horizontal tab - \\t\n");
break;
case 6:
printf("Backslash - \\ \n");
break;
case 7:
printf("Single quotation mark - \\' \n");
break;
case 8:
printf("Double quotation mark - \" \n");
break;
default:
printf("Wrong Input!!\n");
break;
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
\a – Bell (beep)
\b – Backspace
\f – Formfeed
\n – New line
\r – Carriage return
\t – Horizontal tab
\ – Backslash
\' – Single quotation mark
\" – Double quotation mark
Enter your input escape sequence:\'
Single quotation mark - \'
\a – Bell (beep)
\b – Backspace
\f – Formfeed
\n – New line
\r – Carriage return
\t – Horizontal tab
\ – Backslash
\' – Single quotation mark
\" – Double quotation mark
Enter your input escape sequence:\'
Single quotation mark - \'
No comments:
Post a Comment