Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,934 questions

51,871 answers

573 users

How to check if a string contains only English letters in C

1 Answer

0 votes
#include <stdio.h>
#include <regex.h>

int is_only_english_letters(const char *s) {
    regex_t regex;
    int retrurnval;

    // compile the regular expression contained 
    // in the string pointed to by the pattern
    retrurnval = regcomp(&regex, "^[a-zA-Z]+$", REG_EXTENDED); 
    if (retrurnval) {
        char errbuf[128];
        regerror(retrurnval, &regex, errbuf, sizeof(errbuf));
        fprintf(stderr, "Regex compilation error: %s\n", errbuf); 
        return 0; 
    }
    
    // compares the null-terminated string specified by string with the 
    // compiled regular expression regex initialized 
    // by a previous call to regcomp()
    retrurnval = regexec(&regex, s, 0, NULL, 0);
    regfree(&regex);

    if (retrurnval == 0) {
        return 1; 
    } else if (retrurnval == REG_NOMATCH) {
        return 0; 
    } else {
        char errbuf[128];
        regerror(retrurnval, &regex, errbuf, sizeof(errbuf));
        fprintf(stderr, "Regex execution error: %s\n", errbuf); 
        return 0; 
    }
}

int main() {
    const char *s1 = "CProgramming";
    printf("%d\n", is_only_english_letters(s1));

    const char *s2 = "CProgrammingパイソン";
    printf("%d\n", is_only_english_letters(s2));

    const char *s3 = ""; 
    printf("%d\n", is_only_english_letters(s3));

    const char *s4 = "C11Programing"; 
    printf("%d\n", is_only_english_letters(s4));

    return 0;
}
 
 
 
/*
run:
 
1
0
0
0
 
*/

 



answered Dec 30, 2024 by avibootz
edited Dec 30, 2024 by avibootz

Related questions

1 answer 72 views
1 answer 96 views
1 answer 110 views
1 answer 94 views
1 answer 114 views
1 answer 110 views
...