Q:

C program to check whether a character is a printable character or not without using library function

0

C program to check whether a character is a printable character or not without using library function

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here, we will create some character variables with initial values. Then we will check a specified character is printable or not without using any library function.

Program:

The source code to check a given character is a printable character or not without using the library function is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to check whether a character
// is a printable character or not
// without using library function

#include <stdio.h>
#include <ctype.h>

int isPunctuation(char ch)
{
    if (ch == '!' || ch == '"' || ch == '#' || ch == '$' || ch == '%' || ch == '&' || ch == '\'' || ch == '(' || ch == ')' || ch == '*' || ch == '+' || ch == ',' || ch == '-' || ch == '.' || ch == '/' || ch == ':' || ch == ';' || ch == '<' || ch == '=' || ch == '>' || ch == '?' || ch == '@' || ch == '[' || ch == '\\' || ch == ']' || ch == '^' || ch == '`' || ch == '{' || ch == '|' || ch == '}')
        return 1;

    return 0;
}

int isAlphaNumeric(char ch)
{
    if ((ch >= '0' & ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
        return 1;

    return 0;
}

int isPrintable(char ch)
{
    if (isAlphaNumeric(ch) || isPunctuation(ch))
        return 1;
    return 0;
}

int main()
{
    char ch1 = 'a';
    char ch2 = 'A';
    char ch3 = 95;

    if (isPrintable(ch1) != 0)
        printf("Given character is a printable character\n");
    else
        printf("Given character is not a printable character\n");

    if (isPrintable(ch2) != 0)
        printf("Given character is a printable character\n");
    else
        printf("Given character is not a printable character\n");

    if (isPrintable(ch3) != 0)
        printf("Given character is a printable character\n");
    else
        printf("Given character is not a printable character\n");

    return 0;
}

Output:

Given character is a printable character
Given character is a printable character
Given character is not a printable character

Explanation:

Here, we created four functions isPunctuation()isAlphanumeric()isPrintable(), and main(). The isPrintable() function is used to check the given character is printable or not.

In the main() function, we created ch1ch2ch3 that is initialized with 'a',  'A', 95. Then we checked characters are printable or not using the isPrintable() function.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
C program to convert a lowercase character into up... >>
<< C program to check a given character is a punctuat...