How to Search and Count Substrings in C?

  • Thread starter Thread starter M.M.M
  • Start date Start date
  • Tags Tags
    String
Click For Summary

Discussion Overview

The discussion revolves around searching for a substring within a string in the C programming language and counting the number of occurrences of that substring. It includes technical explanations and clarifications regarding the appropriate functions to use.

Discussion Character

  • Technical explanation, Conceptual clarification

Main Points Raised

  • One participant asks how to search for a substring within another string and count its occurrences, expressing uncertainty about the counting aspect.
  • Another participant suggests using the function 'substr' to find the first instance of a substring and recommends calling it repeatedly to count occurrences.
  • A different participant corrects the previous suggestion, indicating that 'strstr' is the appropriate function in C for this purpose, not 'substr'.
  • Another participant acknowledges the mix-up and clarifies that while 'string.find' and 'string.substr' are used in C++, 'strstr' is the correct function in C.

Areas of Agreement / Disagreement

There is no consensus on the initial suggestion to use 'substr', as it is corrected by other participants who emphasize the use of 'strstr' in C. The discussion reflects differing views on the appropriate functions to use for substring searching.

Contextual Notes

The discussion does not resolve the specifics of implementing the counting mechanism or the handling of edge cases in substring searching.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K
Replies
5
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
55
Views
7K
Replies
2
Views
3K
Replies
89
Views
7K
  • · Replies 22 ·
Replies
22
Views
4K