fork download
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include <regex.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. int main (int argc, char *argv[])
  8. {
  9. /*
  10.   if (argc < 2) {
  11.   fprintf(stderr, "Usage: %s number\n", argv[0]);
  12.   return(1);
  13.   }
  14.  
  15.   int num = atoi(argv[1]);
  16.   */
  17. int num=41;
  18. printf("%d\n",num);
  19.  
  20. regex_t regex;
  21. char buf [255];
  22. char str [num+1];
  23.  
  24. memset(str,'1',num);
  25. str[num] = 0;
  26. puts(str);
  27.  
  28. regcomp(&regex, "^(11+)\\1+$", REG_EXTENDED);
  29.  
  30. int r = regexec(&regex, str, 0, NULL, 0);
  31.  
  32. //If a match is found, the regexec() function returns 0.
  33. if (0 == r) {
  34. puts("Not Prime");
  35. }
  36. //If no match is found, the regexec() function returns REG_NOMATCH.
  37. else if (REG_NOMATCH == r) {
  38. puts("Prime");
  39. }
  40. //Otherwise, it returns a nonzero value indicating an error.
  41. else {
  42. regerror(r, &regex, buf, sizeof(buf));
  43. fprintf(stderr, "Error: %d %s\n",r , buf);
  44. return(1);
  45. }
  46.  
  47. }
  48.  
Success #stdin #stdout 4.31s 4508KB
stdin
Standard input is empty
stdout
41
11111111111111111111111111111111111111111
Prime