Check if a String Follows Snake Case in C
To check if a string follows snake case in C, you can iterate through the string and verify that each character meets the snake case criteria: only lowercase letters, underscores, and optionally digits are allowed, with no leading, trailing, or consecutive underscores.
Examples to Check if a String Follows Snake Case
1. Using a For Loop and ctype.h
Functions
This example demonstrates how to check snake case using a for
loop. We create a function isSnakeCase
that iterates over the string and validates each character using functions from ctype.h
, ensuring proper snake case formatting.
main.c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int isSnakeCase(const char *str) {
int len = strlen(str);
if (len == 0) return 0; // Empty string is not valid
if (str[0] == '_' || str[len - 1] == '_') return 0; // Should not start or end with '_'
for (int i = 0; i < len; i++) {
char ch = str[i];
// Check if character is either a lowercase letter, underscore, or digit
if (!(islower(ch) || ch == '_' || isdigit(ch))) {
return 0;
}
// Check for consecutive underscores
if (ch == '_' && i > 0 && str[i - 1] == '_') {
return 0;
}
}
return 1;
}
int main() {
char str1[] = "hello_world";
char str2[] = "Hello_world";
printf("'%s' is snake case? %s\n", str1, isSnakeCase(str1) ? "Yes" : "No");
printf("'%s' is snake case? %s\n", str2, isSnakeCase(str2) ? "Yes" : "No");
return 0;
}
Explanation:
- The function
isSnakeCase
calculates the string length usingstrlen
and checks if the string is empty. - It ensures that the string does not start or end with an underscore.
- A
for
loop iterates over each character. For every character,islower()
verifies lowercase letters,isdigit()
checks for digits, and underscores are allowed. Consecutive underscores are disallowed by comparing the current character with the previous one. - The
main
function tests the implementation with two strings:"hello_world"
(valid) and"Hello_world"
(invalid due to an uppercase letter).
Output:
'hello_world' is snake case? Yes
'Hello_world' is snake case? No
2. Using Pointer Iteration with a While Loop
This example uses pointer iteration with a while
loop. The function isSnakeCase
traverses the string using pointer arithmetic, checking each character for compliance with snake case rules while ensuring no invalid patterns (like consecutive or trailing underscores) are present.
main.c
#include <stdio.h>
#include <ctype.h>
int isSnakeCase(const char *str) {
if (!str || !*str) return 0; // Check for NULL or empty string
if (*str == '_') return 0; // Should not start with '_'
char prev = '\0';
while (*str) {
char ch = *str;
// Check if character is lowercase, an underscore, or a digit
if (!(islower(ch) || ch == '_' || (ch >= '0' && ch <= '9'))) {
return 0;
}
// Check for consecutive underscores
if (ch == '_' && prev == '_') {
return 0;
}
prev = ch;
str++;
}
if (prev == '_') return 0; // Should not end with '_'
return 1;
}
int main() {
const char *testStr = "apple_banana_cherry";
printf("'%s' is snake case? %s\n", testStr, isSnakeCase(testStr) ? "Yes" : "No");
return 0;
}
Explanation:
- The function first checks if the input string is NULL or empty, and verifies that it does not start with an underscore.
- A
while
loop is used with pointer arithmetic to traverse the string. - Each character is validated to be a lowercase letter using
islower()
, an underscore by direct comparison, or a digit using a range check. - The variable
prev
stores the previous character to detect any consecutive underscores. - After the loop, it confirms that the string does not end with an underscore.
- The
main
function tests this logic with the string"check_snake_case"
, which should be valid.
Output:
'apple_banana_cherry' is snake case? Yes
Conclusion
In this tutorial, we explored two approaches to check if a string follows snake case in C. The first method used a for
loop combined with standard library functions, while the second method employed pointer iteration with a while
loop.