zoukankan      html  css  js  c++  java
  • C语言字符串查找函数

      1 #include <string.h>
      2 #include <stdio.h>
      3 
      4 char * string_search(char long_str[], char short_str[])
      5 {//author: emanlee
      6     char *pl, *qs;
      7     long is_identical, long_length, short_length;
      8     long position, ii;
      9     
     10     long_length=strlen(long_str);
     11     short_length=strlen(short_str);
     12     
     13     if (long_length<short_length)
     14     {
     15         printf("ERROR
    ");
     16         return NULL;
     17     }
     18     
     19     for (position=0; position<long_length-short_length+1; position++)
     20     {
     21         pl=long_str+position;
     22         qs=short_str;
     23         is_identical=1;
     24         
     25         for (ii=0; ii<short_length; ii++)
     26         {
     27             if (*pl != *qs)
     28             {
     29                 is_identical=0;
     30                 break;
     31             }
     32             
     33             pl++;
     34             qs++;
     35         }
     36         
     37         if (is_identical==1)
     38         {
     39             return long_str+position;    
     40         }
     41         
     42         
     43     }
     44 
     45     return NULL;
     46     
     47     
     48 }
     49 
     50 
     51 char * string_search_postion(char long_str[], char short_str[], long start_position)
     52 {//author: emanlee
     53     char *pl, *qs;
     54     long is_identical, long_length, short_length;
     55     long position, ii;
     56     
     57     long_length=strlen(long_str);
     58     short_length=strlen(short_str);
     59     
     60     if (long_length<short_length)
     61     {
     62         printf("ERROR
    ");
     63         return NULL;
     64     }
     65     
     66     for (position=start_position; position<long_length-short_length+1; position++)
     67     {
     68         pl=long_str+position;
     69         qs=short_str;
     70         is_identical=1;
     71         
     72         for (ii=0; ii<short_length; ii++)
     73         {
     74             if (*pl != *qs)
     75             {
     76                 is_identical=0;
     77                 break;
     78             }
     79             
     80             pl++;
     81             qs++;
     82         }
     83         
     84         if (is_identical==1)
     85         {
     86             return long_str+position;    
     87         }
     88         
     89         
     90     }
     91     
     92     return NULL;
     93     
     94     
     95 }
     96 
     97 
     98 
     99 char * string_search_pointer_postion(char long_str[], char short_str[], char * start_position)
    100 {//author: emanlee
    101     char *pl, *qs;
    102     long is_identical, long_length, short_length;
    103     long position, ii;
    104     
    105     long_length=strlen(long_str);
    106     short_length=strlen(short_str);
    107     
    108     if (long_length<short_length)
    109     {
    110         printf("ERROR
    ");
    111         return NULL;
    112     }
    113     
    114     for (position=(long)(start_position-long_str); position<long_length-short_length+1; position++)
    115     {
    116         pl=long_str+position;
    117         qs=short_str;
    118         is_identical=1;
    119         
    120         for (ii=0; ii<short_length; ii++)
    121         {
    122             if (*pl != *qs)
    123             {
    124                 is_identical=0;
    125                 break;
    126             }
    127             
    128             pl++;
    129             qs++;
    130         }
    131         
    132         if (is_identical==1)
    133         {
    134             return long_str+position;    
    135         }
    136         
    137         
    138     }
    139     
    140     return NULL;
    141     
    142     
    143 }
    144 
    145 
    146 void main()
    147 {
    148     char a[1000]="12345678901234567890";
    149     char b[1000]="456";
    150     char *p;
    151 
    152     p=string_search(a,b);
    153 
    154     printf("%s
    ", string_search(a,b));
    155     printf("%s
    ", string_search_postion(a,b,9));
    156     printf("%s
    ", string_search_pointer_postion(a,b,p+1));
    157 
    158 }
  • 相关阅读:
    Pytest之自定义mark
    Pytest之skip、skipif、xfail
    Pytest之fixture
    Pytest之断言
    Pytest之收集用例及命令行参数
    Pytest之基本介绍
    Python接口自动化之动态数据处理
    Python接口自动化之接口依赖
    关于激光纵模、横模与锁模技术的理解 Label:Research
    如何快速设计一个FIR滤波器 Label:Research
  • 原文地址:https://www.cnblogs.com/emanlee/p/3475125.html
Copyright © 2011-2022 走看看