ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C program to check a given character is a punctuation mark or not without using the library function

Here, we are going to learn how to check a given character is a punctuation mark or not without using the library function in C programming language?
Submitted by Nidhi, on July 16, 2021

Problem Solution:

Given a character, we have to check whether the given character is a punctuation mark or not without using the library function.

Program:

The source code to check a given character is a punctuation mark 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 a given character is a punctuation mark or not
// without using library function

#include <stdio.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 main()
{
    char ch;

    printf("Enter character: ");
    scanf("%c", &ch);

    if (isPunctuation(ch))
        printf("Given character is a punctuation mark\n");
    else
        printf("Given character is not a punctuation mark\n");

    return 0;
}

Output:

RUN 1:
Enter character: ?
Given character is a punctuation mark

RUN 2:
Enter character: !
Given character is a punctuation mark

RUN 3:
Enter character: a
Given character is not a punctuation mark

Explanation:

In the above program, we created two functions isPunctuation() and main(). The isPunctuation() function is used to check the given character is a punctuation mark or not.

In the main() function, we read a character from the user and check given character is a punctuation mark or not by calling the isPunctuation() function and print the appropriate message on the console screen.

C Basic Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.