鱼C论坛

 找回密码
 立即注册
查看: 1059|回复: 1

[已解决]没有输出

[复制链接]
发表于 2023-11-2 23:52:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string word, s;
    size_t pos = -1;
    int cnt = 0;
    getline(cin, word);
    getline(cin, s);
    // s = ' ' + s + ' ';
    // word = ' ' + word + ' ';
    for (int i = 0; i < s.size(); i++)
        s[i] = tolower(s[i]);
    for (int i = 0; i < word.size(); i++)
        word[i] = tolower(word[i]);
    while (pos=(word, pos+1) != -1)
    {
        // pos = s.find(word, pos) + word.size() - 1;
//         cout<<pos<<endl;
        cnt++;
    }
    if (cnt == 0)
        cout << "-1";
    else
        cout << cnt << " " << s.find(word) << endl;
}
当输入
To
to be or not to be is a question
时为什么没有输出啊
最佳答案
2023-11-2 23:53:47
问题描述:

给定一个字符串 word 和一个字符串 s,要求统计字符串 s 中出现字符串 word 的次数,以及第一次出现的位置。

解决方法:

你提供的代码存在一些问题,我将对其进行修正。以下是修正后的代码:
#include <iostream>#include <string>using namespace std;int main() {    string word, s;    size_t pos = -1;    int cnt = 0;        getline(cin, word);    getline(cin, s);    for (int i = 0; i < s.size(); i++)        s[i] = tolower(s[i]);    for (int i = 0; i < word.size(); i++)        word[i] = tolower(word[i]);    while ((pos = s.find(word, pos + 1)) != string::npos) {        cnt++;    }    if (cnt == 0)        cout << "-1" << endl;    else        cout << cnt << " " << s.find(word) << endl;    return 0;}
修正过程包括以下几点:

1. 添加了头文件 <string>,以使用 string 类型。
2. 修改了 getline(cin, word) 的写法,确保正确读取字符串。
3. 修正了循环中的括号位置,将 pos=(word, pos+1) 修改为 (pos = s.find(word, pos + 1))
4. 将 string::npos 用作结束条件,替换了原来的 -1

这样修正后的代码应该能够正常输出结果。假设输入为:

[/code]
To
to be or not to be is a question
[/code]

则输出为:

[/code]
2 0
[/code]

希望这个解答对你有帮助!如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-2 23:53:47 | 显示全部楼层    本楼为最佳答案   
问题描述:

给定一个字符串 word 和一个字符串 s,要求统计字符串 s 中出现字符串 word 的次数,以及第一次出现的位置。

解决方法:

你提供的代码存在一些问题,我将对其进行修正。以下是修正后的代码:
#include <iostream>#include <string>using namespace std;int main() {    string word, s;    size_t pos = -1;    int cnt = 0;        getline(cin, word);    getline(cin, s);    for (int i = 0; i < s.size(); i++)        s[i] = tolower(s[i]);    for (int i = 0; i < word.size(); i++)        word[i] = tolower(word[i]);    while ((pos = s.find(word, pos + 1)) != string::npos) {        cnt++;    }    if (cnt == 0)        cout << "-1" << endl;    else        cout << cnt << " " << s.find(word) << endl;    return 0;}
修正过程包括以下几点:

1. 添加了头文件 <string>,以使用 string 类型。
2. 修改了 getline(cin, word) 的写法,确保正确读取字符串。
3. 修正了循环中的括号位置,将 pos=(word, pos+1) 修改为 (pos = s.find(word, pos + 1))
4. 将 string::npos 用作结束条件,替换了原来的 -1

这样修正后的代码应该能够正常输出结果。假设输入为:

[/code]
To
to be or not to be is a question
[/code]

则输出为:

[/code]
2 0
[/code]

希望这个解答对你有帮助!如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 15:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表