1

Is there function in a C library that iterates through an array and checks if two characters are next to each other?

For example : array[30] = "example@.com"
Is it possible to go through the array and check if '@' and '.' are next to each other?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 2
    Are you just looking for `strstr(array, "@." ) || strstr(array,".@")`? But you'd (probably) be better off writing specific code to check it rather than making strstr scan the array twice. – William Pursell Dec 17 '18 at 22:29
  • Way 1:Step through the string a character at a time. If you see a '@', record its position (either as an index or a pointer). If you see a '.', record its position. Also, each time you see a '@' or a '.', check to see if you already have a position of the other one, and if it's just before (1 less than) the position you're at. – Steve Summit Dec 17 '18 at 22:32
  • Way 2: Use `strchr` to find the position of the first '@'. Use it again to find the position of the first '.'. See if the two positions differ by 1. (This will give the wrong answer, though, for a string like `"ab@cd.ef@.gh"`.) – Steve Summit Dec 17 '18 at 22:34
  • @SteveSummit already used strchr in another part of my code, needed another function for checking two characters next to each other! – Math_Seeker Dec 17 '18 at 22:36
  • I would suggest using a regular expression. They aren't built into C, but this post https://stackoverflow.com/questions/1085083/regular-expressions-in-c-examples suggests some libraries to help out. – Tom Johnson Dec 17 '18 at 22:37
  • @TomJohnson Thank you for your suggestion, unfortunately I am unfamiliar with regex, and it isn't something I can use in my code. But thank you again! – Math_Seeker Dec 17 '18 at 22:39
  • " if two characters are next to each other?' Can either one of the 2 characters be `'\0'`? – chux - Reinstate Monica Dec 17 '18 at 22:47
  • @chux I am not sure its possible since \0 only occurs at the end of a char string. I could be mistaken, I am still new to coding in general. – Math_Seeker Dec 17 '18 at 22:49
  • @Math_Seeker Yes a _string_ has one and only one _null character_ at its end. Yet you still may want to do `pair_check("example@.com", 'x', '\0')`, in which case the `strstr()` idea will not work. It is your call. – chux - Reinstate Monica Dec 17 '18 at 22:53
  • @chux I will try that. Thank you! – Math_Seeker Dec 17 '18 at 22:55

2 Answers2

1

Use strstr:

if (strstr(array, "@.") || strstr(array, ".@"))
    /* the characters are touching */
lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
1

Is there function in a C library that iterates through an array and checks if two characters are next to each other?

No.

As OP asked about an array and not a string, a strstr() approach will not work.

Use the below, works even if either/both c2, c2 are '\0'.

bool two_char_check(const char *s, size_t n, char c1, char c2) {
  const char *original = s;
  while (n > 0) {
    char *s1 = memchr(s, c1, n);
    if (s1 == NULL) {
      return false;
    }
    if (s1 != original && s1[-1] == c2) {
      return true;
    }
    size_t offset2 = (size_t) (s1 - s) + 1;
    if (offset2 < n && s1[1] == c2) {
      return true;
    }
    s += offset2;
    n -= offset2;
  }
  return false;
}

int main(void) {
  char array[30] = "example@.com";
  // expect true
  printf("%d\n", two_char_check(array, sizeof array, '@', '.'));
  printf("%d\n", two_char_check(array, sizeof array, 'm', '\0'));
  printf("%d\n", two_char_check(array, sizeof array, '\0', 'm'));
  printf("%d\n", two_char_check(array, sizeof array, '\0', '\0'));

  // expect false
  printf("%d\n", two_char_check(array, sizeof array, 'x', '\0'));
  printf("%d\n", two_char_check(array, sizeof array, '@', 'E'));
  printf("%d\n", two_char_check(array, sizeof array, 'M', '\0'));
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256