Data Structure-algorithm (028) (search for the number of substrings)

Source: Internet
Author: User

[Statement: This article is only intended for self-Summary and mutual communication, and may be omitted. Email: Mr_chenping@163.com]

Question:

A known string, such as asderwsde, is used to search for a substring, such as the number of sde. If no value is returned, the number of sub-strings is returned.
Question Analysis:

1. traverse the string sequentially and then compare it with strncmp.

Algorithm Implementation:

# Include

 
  
# Include

  
   
Int sub_str_count (const char * str, const char * sub_str) {int str_len = strlen (str); int sub_str_len = strlen (sub_str); int count = 0; int times = str_len-sub_str_len + 1; while (times --) {if (! Strncmp (str, sub_str, sub_str_len) count ++; str ++;} return count ;} int main (int argc, char * argv []) {printf ("% s ---- % s ---> % d \ n", argv [1], argv [2], sub_str_count (argv [1], argv [2]); return 0 ;}

  

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.