c++ 给出一串字符 再给出一个字符 统计第二次字符在第一次出现多少次 帮忙改一下

来源:百度知道 编辑:UC知道 时间:2024/06/05 05:25:47
自己运行没错 可是就是调试不出来 谢谢各位了 要详细啊 详细 我比较菜
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1,str2,str3,temp;
cout<<"请输入一串字符:"<<endl;
cin>>str1;cout<<endl;
cout<<"请输入要统计的字符串:"<<endl;
cin>>str2;cout<<endl;
int m,n,i,j=0,k=0;
m=str1.length();
n=str2.length();
string *p;
p=&str1;
for(i=0;i<m-1;i++)
{
p+=j*sizeof(char);
j++;
temp=*(p+n*sizeof(char));
*(p+n)='\0';
str3=*p;
*(p+n*sizeof(char))=temp;
if(str3==str2)
k++;
n++;
}
if(k=0) cout<<"没有匹配的字符!"<<endl;
else cout<<"您所统计的字符共出现"<<k<<"次"<<endl;
return 0;
}
错了 c++ 给出一串字符 再给出一“串”字符 不好意思

自己运行没错 可是就是调试不出来...何解?

OK, 了解了,下面是我的方法...

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1;
string str2;
int count = 0;
cin >> str1;
cin >> str2;
int len = str2.length();
if( str1.length() < str2.length())
{
cout << "Input Erro!" << endl;
system("pause");
return 0;
}
for(int i = 0;i <= str1.length()-len;++i)
{
if( str1.substr(i,len) == str2 )
{
count++;
}
}
if(0 == count)
{
cout << "No result." << endl;
}
else
{
cout << "Result is " << count
<< "times" << endl;
}
system("pause");
return 0;