Check if a String Contains Only Punctuation Marks in C
To check if a string contains only punctuation marks, iterate through each character and verify if it is a punctuation symbol. In C, you can achieve this using the ispunct()
function from <ctype.h>
or by manually comparing each character against a set of known punctuation marks.
Example 1: Using ispunct()
with a For Loop
In this example, we are going to use the standard library function ispunct()
to check if every character in a string is a punctuation mark. We iterate over the string using a for
loop.
Program:
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "!@#$%^&*()"; // String containing only punctuation marks
int flag = 1; // Assume the string is all punctuation
// Iterate over each character in the string
for (int i = 0; str[i] != '\0'; i++) {
if (!ispunct(str[i])) { // Check if character is not a punctuation mark
flag = 0;
break;
}
}
if (flag)
printf("The string contains only punctuation marks.\n");
else
printf("The string contains non-punctuation characters.\n");
return 0;
}
Explanation:
- The string
str
is initialized with punctuation characters. - The variable
flag
is set to 1, assuming the string is valid. - A
for
loop iterates through each character ofstr
until the null terminator ('\0'
) is encountered. - The function
ispunct()
checks if the current character is a punctuation mark. If not,flag
is set to 0 and the loop breaks. - After the loop, the program prints the result based on the value of
flag
.
Output:
The string contains only punctuation marks.
Example 2: Manual Check Against a List of Punctuation Marks
In this example, we are going to manually check if each character in the string is one of the predefined punctuation marks by using a helper function.
Program:
#include <stdio.h>
#include <string.h>
int is_punctuation(char c) {
// List of common punctuation marks
char punct[] = "!@#$%^&*()-_+=[]{};:'\",.<>/?\\|`~";
for (int i = 0; punct[i] != '\0'; i++) {
if (c == punct[i])
return 1;
}
return 0;
}
int main() {
char str[] = ".,;:!?"; // String containing only punctuation marks
int flag = 1;
// Check each character manually
for (int i = 0; str[i] != '\0'; i++) {
if (!is_punctuation(str[i])) {
flag = 0;
break;
}
}
if (flag)
printf("The string contains only punctuation marks.\n");
else
printf("The string contains non-punctuation characters.\n");
return 0;
}
Explanation:
- The helper function
is_punctuation()
checks if a character exists in a predefined string of punctuation marks. - The string
str
is initialized with punctuation symbols. - A
for
loop iterates over every character instr
. - For each character, the
is_punctuation()
function is called. If a character is not found in the list,flag
is set to 0 and the loop is terminated. - Finally, the program prints whether the string contains only punctuation marks based on the value of
flag
.
Output:
The string contains only punctuation marks.
Example 3: Using Pointer Arithmetic with ispunct()
In this example, we will use pointer arithmetic to traverse the string and use the ispunct()
function to check each character.
Program:
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "!?.,()"; // String containing only punctuation marks
int flag = 1;
char *ptr = str; // Pointer to traverse the string
// Traverse the string using pointer arithmetic
while (*ptr != '\0') {
if (!ispunct(*ptr)) {
flag = 0;
break;
}
ptr++;
}
if (flag)
printf("The string contains only punctuation marks.\n");
else
printf("The string contains non-punctuation characters.\n");
return 0;
}
Explanation:
- The string
str
is defined with punctuation characters. - A pointer
ptr
is initialized to point to the beginning ofstr
. - A
while
loop is used to traverse the string until the null terminator ('\0'
) is reached. - At each iteration, the character pointed to by
ptr
is checked withispunct()
. If it is not a punctuation mark,flag
is set to 0 and the loop is exited. - After the loop, the result is printed based on the value of
flag
.
Output:
The string contains only punctuation marks.
Conclusion
In this tutorial, we explored multiple approaches to verify if a string contains only punctuation marks in C. We used the built-in ispunct()
function with both a for
loop and pointer arithmetic, as well as a manual method with a helper function to compare each character against a predefined list of punctuation symbols.