How to check String in startwith function?

i have one buffer//unsigned char *buffer =NULL. it is dynamic buffer . run time only get size/length .

i want to check starting string only.

previously i was used in python function like "startswith".

is any function possible in c++?

Regards,
Kuluoz
One Way, however works only for case-sensitive strings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstring>

using namespace std;

bool startsWith(const char *s1, const char *s2)
{
  if (s1 == nullptr || s2 == nullptr)
    return false;

  return strstr(s1, s2) == s1;
}

int main(int argc, char *argv[])
{
  const char *input = "Hello world";
  const char *search = "Hello";

  if (startsWith(input, search))
  {
    cout << "\nYes";
  }
  else
  {
    cout << "\nNo";
  }

  system("pause");
  return 0;
}

Thanks. It is work.
Topic archived. No new replies allowed.