Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the text starting with another text

Tags:

c

string

Is there any method in C can find a text within another text?

For example, text = "abaHello", textneedtoSearch = "Hello";.

If the text contains "Hello", return true, else return false.

like image 801
hkvega Avatar asked Jun 03 '11 07:06

hkvega


3 Answers

Use strstr, see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strstr.html

like image 190
csl Avatar answered Nov 20 '22 06:11

csl


Character and string searching functions

`char *strstr( const char *s1,  const char *s2)`

returns a pointer to the first instance of string s2 in s1. Returns a NULL pointer if s2 is not encountered in s1.


In additon,

int strcmp(const char *s1, const char *s2);

strcmp compares the string s1 to the string s2. The function returns 0 if they are the same, a number < 0 if s1 < s2, a number > 0 if s1 > s2.

This is one of the most commonly used of the string-handling functions.

And check this link for anything about string functions in C, C string functions

like image 32
Bastardo Avatar answered Nov 20 '22 05:11

Bastardo


if (strstr(text, textneedtoSearch) != NULL)
  printf("found\n");
like image 3
patapizza Avatar answered Nov 20 '22 05:11

patapizza