How to Search and Count Substrings in C?

  • Thread starter Thread starter M.M.M
  • Start date Start date
  • Tags Tags
    String
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
M.M.M
Messages
24
Reaction score
0
Hello every body..

are you okey..

i have a question in C about :

how can i search a string inside another string and count the number of the occurrences of that string ?

i think the problem is in counting

is there any suggestion ?
 
Physics news on Phys.org
You can use substr to find the first instance of a substring within a string. Repeatedly call this function on the new starting position until it fails, to count the number of occurrences.
 
I think you mean http://www.cplusplus.com/reference/clibrary/cstring/strstr/ not substr.
 
Yeah, messed that up...in C++ you would use string.find and string.substr, but in C you have strstr.