50分求大佬
查看原帖
50分求大佬
1089623
aouyou楼主2024/10/14 20:52

实在找不到错误求指点

#include <iostream>
#include <string>
using namespace std;
string a, s;
int cnt = 0, pos;
int main()
{	//输入字符串
	getline(cin, a);
	getline(cin, s);
  	 //全部变为小写
	for (int i = 0; i < a.length(); i++)
	{
		if (a[i] >= 65 && a[i] <= 90)
		{
			a[i] += 32;
		}
	}
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] >= 65 && s[i] <= 90)
		{
			s[i] += 32;
		}
	}
   	//找第一个字符的位置
	int f = s.find(a, 0);
	if (f == 0 && s[f + a.length()] == ' ')
	{
		cnt++;
		pos = f;
	}
	else if (f > 0 && s[f - 1] == ' ' && s[f + a.length()] == ' ')
	{
		cnt++;
		pos = f;
	}
	else
	{
		cout << "-1" << endl;
		return 0;
	}
        //找之后的位置
	while (1)
	{
		pos = s.find(a, pos + a.length());
		if (pos == -1)
		{
			break;
		}
		else if (s[pos - 1] == ' ' && (s[pos + a.length()] == ' ' || s[pos + a.length() == NULL]))
			cnt++;
	}
	cout << cnt  << " " << f << endl;
	return 0;
}
2024/10/14 20:52
加载中...