How to find the first occurrence of one of several characters (other than using regex)

StackOverflow https://stackoverflow.com/questions/12845923

  •  06-07-2021
  •  | 
  •  

Question

After going through a bunch of threads, I know that I need to use regex.h for using regular expressions in C & C++.

But I was wondering, is there an easier way to search for occurrence of "/" or "\" in a string.

// I have a strstr statement like this -
str = strstr(s, "/");

I was wondering if it is possible to change it so that I can search for the first occurrence of a / or \ in a single call to strstr.

Was it helpful?

Solution

Try strcspn:

Get span until character in string
Scans str1 for the first occurrence of any of the characters that are part of str2, returning the number of characters of str1 read before this first occurrence.

The search includes the terminating null-characters. Therefore, the function will return the length of str1 if none of the characters of str2 are found in str1.

Example:

#include <stdio.h>
#include <string.h>

const char* findany(const char* s, const char* keys)
{
    const char* tmp;
    tmp = s + strcspn(s,keys);
    return *tmp == '\0' ? NULL : tmp;
}

int main ()
{
  char str1[] = "abc\\123";
  char str2[] = "abc/123";
  char str3[] = "abc123";
  char keys[] = "/\\";
  printf("1: %s\n",findany(str1,keys));
  printf("2: %s\n",findany(str2,keys));
  printf("3: %s\n",findany(str3,keys));
  return 0;
}

Edit: strpbrk does the same thing as findany above. Didn't see that function:

#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[] = "abc\\123";
  char str2[] = "abc/123";
  char str3[] = "abc123";
  char keys[] = "/\\";
  printf("1: %s\n",strpbrk(str1,keys));
  printf("2: %s\n",strpbrk(str2,keys));
  printf("3: %s\n",strpbrk(str3,keys));
  return 0;
}

Output (of both):

1: \123
2: /123
3: (null)

OTHER TIPS

There's a C function that does just that - strpbrk()

I was wondering if it is possible to change it so that I can search for first occurrence of "/" or "\" in a single call to strstr

You can use strtok, which modifies the source string (or see strtok_r). Or you can use strcspn:

The strcspn() function shall compute the length (in bytes) of the maximum
initial segment of the string pointed to  by  s1  which consists entirely
of bytes not from the string pointed to by s2.

So

p = s + strcspn(s, "/\\");

will return a pointer either to /, \, or to the final NUL character (if neither \ nor // were found).

Of course you can also run strchr twice and see which one, if not NULL, is first.

You could try using the strchr function to find the first occurrence of a character:

pos_of_first_slash=strchr(s, '/');

Returns a pointer to the first occurrence of character in the C string str.

The terminating null-character is considered part of the C string. Therefore, it can also be located in order to retrieve a pointer to the end of a string.

Returns a pointer to the first occurrence of character in str. If the character is not found, the function returns a null pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top