Srting inside another string ?

  • Thread starter M.M.M
  • Start date
  • Tags
    String
In summary, the conversation was about searching and counting occurrences of a substring within a string in C. The suggestion was to use substr or strstr function to find the first instance and repeatedly call it to count the occurrences. It was also mentioned that in C++, string.find and string.substr would be used instead.
  • #1
M.M.M
24
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
  • #2
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.
 
  • #3
I think you mean http://www.cplusplus.com/reference/clibrary/cstring/strstr/ not substr.
 
  • #4
Yeah, messed that up...in C++ you would use string.find and string.substr, but in C you have strstr.
 

Related to Srting inside another string ?

1. What does it mean for a string to be inside another string?

For a string to be inside another string means that the first string is a part of the second string. This is also known as substring or subsequence.

2. How can I determine if a string is inside another string?

You can use the indexOf() method in most programming languages to check if a string is inside another string. If the method returns a value of -1, then the string is not present. If it returns a value greater than -1, then the string is present.

3. Can I have multiple instances of the same string inside another string?

Yes, it is possible to have multiple instances of the same string inside another string. The indexOf() method will only return the first index of the string, so you may need to use a loop to check for all instances.

4. How can I extract the string inside another string?

You can use the substring() method to extract a specific portion of a string inside another string. This method takes in two parameters, the starting index and the ending index, and returns the extracted string.

5. Is there a way to replace a string inside another string?

Yes, you can use the replace() method to replace a string inside another string. This method takes in two parameters, the string to be replaced and the string to replace it with, and returns the modified string.

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
4
Replies
118
Views
7K
  • Programming and Computer Science
Replies
28
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
404
Back
Top