zoukankan      html  css  js  c++  java
  • C语言:利用指针解决:统计一个长度为2的字符串在另外一个字符串中出现的次数。

    //统计一个长度为2的字符串在另外一个字符串中出现的次数。

     1 #include <conio.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 int fun(char *str, char *substr)
     6 {
     7     char *z, *c;
     8     z = str;
     9     c = substr;
    10     int n=0;
    11     while (*z!='')
    12     {
    13         if (*z== *c)
    14         {
    15             z++; c++;
    16             if (*z == *c)
    17             {
    18                 n++;
    19             }
    20             c--;
    21         }
    22         else
    23         {
    24             z++;
    25         }
    26     }
    27     return n;
    28 }
    29 void main()
    30 { 
    31   FILE *wf;
    32   char str[81],substr[3];
    33   int n;
    34   system("CLS");
    35   printf("输入主字符串: ");
    36   gets(str);
    37   printf("输入子字符串: ");
    38   gets(substr);
    39   puts(str);
    40   puts(substr);
    41   n=fun(str,substr);
    42   printf("n=%d
     ",n);
    43 /******************************/
    44   wf=fopen("out.dat","w");
    45   n=fun("asd asasdfg asd as zx67 asd mklo","as");
    46   fprintf(wf,"%d",n);
    47   fclose(wf);
    48 /*****************************/
    49 }
  • 相关阅读:
    DS博客作业05--查找
    DS博客作业04--图
    DS博客作业03--树
    栈和队列
    第六次作业
    第五次作业
    第四次作业
    第三次作业
    java任务
    第三周-自主学习任务-面向对象基础与类的识别
  • 原文地址:https://www.cnblogs.com/ming-4/p/10517482.html
Copyright © 2011-2022 走看看