How to Search and Count Substrings in C?

  • Thread starter Thread starter M.M.M
  • Start date Start date
  • Tags Tags
    String
AI Thread Summary
To search for a substring within a string in C and count its occurrences, the function `strstr` can be utilized. This function locates the first instance of the substring. To count all occurrences, repeatedly call `strstr` by updating the starting position to just after the last found instance until no more instances are found. While `substr` is relevant in C++, in C, `strstr` is the correct function to use for this task.
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 ?
 
Technology 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top