字符串查找匹配位置-在输入的字符串中查找特定字符,查到返回位置和个数

一、代码

 1 //在输入的字符串中查找特定字符,查到返回位置和个数
 2 #include <iostream>
 3 using namespace std;
 4 int mystrchr(char string[],char c)
 5 {
 6     int len=strlen(string);//判断字符串长度
 7     int count=0;//记录c的个数
 8     cout<<"输入的字符串长度为:"<<len<<endl;
 9     for (int i=0;i<len;i++)
10     {
11         if(string[i]==c)
12         {  
13            cout<<c<<"的匹配位置为:"<<i+1<<endl;
14            count++;
15         }
16     }
17     cout<<"字符"<<c<<"的个数为:"<<count<<endl;
18     return 0;
19 }
20 void main()
21 {
22     cout<<"请输入字符串:"<<endl;
23     char a[20];
24     cin>>a;
25     cout<<"请输入需要查找的字符:"<<endl;
26     char m;
27     cin>>m;
28     mystrchr(a,m);
29 }

二、演示

原文地址:https://www.cnblogs.com/f59130/p/3328515.html