英文字符串中单词的统计和计
时间: 2024-03-24 18:32:58 浏览: 7
以下是统计英文字符串中单词个数的两种方法:
1. 使用split()函数和len()函数统计单词个数:
```python
s = input()
ls = s.split()
print(len(ls))
```
2. 使用循环和判断条件统计单词个数:
```python
import string
s = input()
count = 0
if s[0] != ' ':
count = 1
for i in range(len(s)):
if s[i] == ' ' and s[i+1] in string.ascii_letters+string.digits:
count = count + 1
print(count)
```
相关问题
请使用C++字符串流结合常用STL容器完成以下功能: 统计这段文字的不重复的单词个数(即多次出现的单词只计为一个)输入一段不换行、含句号、逗号、双引号的英文文字。 输出 按单词首字母A-Z排序,一行一词输出小写字母和频数。
下面是C++的实现代码:
```c++
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
string text;
getline(cin, text);
// 去除标点符号
for (int i = 0; i < text.length(); i++) {
if (ispunct(text[i])) {
text.erase(i--, 1);
}
}
// 转换为小写
transform(text.begin(), text.end(), text.begin(), ::tolower);
// 统计单词频数
map<string, int> word_freq;
istringstream iss(text);
string word;
while (iss >> word) {
word_freq[word]++;
}
// 按单词首字母排序
set<string> words;
for (const auto& p : word_freq) {
words.insert(p.first);
}
// 输出结果
for (char c = 'a'; c <= 'z'; c++) {
for (const auto& word : words) {
if (word[0] == c) {
cout << word << " " << word_freq[word] << endl;
}
}
}
return 0;
}
```
解释:
首先读入一行文本,然后去除标点符号,并转换为小写字母。接着使用istringstream将文本分割为单词,统计每个单词的出现频数并保存在map容器word_freq中。然后使用set容器words保存所有不重复的单词,并按照首字母排序。最后按照字母顺序和单词频数输出结果。
p1308 [noip2011 普及组] 统计单词数
### 回答1:
题目描述:
给定一个英文文章,统计其中不同单词的个数。
输入格式:
输入文件包含若干行,每行是一个由大小写字母、逗号、句号和空格组成的字符串,表示一篇英文文章。每个字符串的长度不超过100。
输出格式:
输出文件仅一行,包含一个整数,表示输入文件中不同单词的个数。不区分大小写,相同的单词计为一个。
输入样例:
This is a test.
Hello world, hello China.
输出样例:
7
解题思路:
本题需要统计文章中不同单词的个数,不区分大小写,相同的单词计为一个。因此,我们可以使用哈希表来统计每个单词出现的次数,最后输出哈希表中不同单词的个数即可。
具体实现时,我们可以先将文章中的所有单词转换为小写字母,然后使用字符串流istringstream将每个单词读入,再将其插入哈希表中。最后,输出哈希表中不同单词的个数即可。
代码实现:
### 回答2:
题目描述
给定一篇英语文章,统计其中单词的数量。单词是指仅由英文字母组成的、不包含数字、标点符号等非字母字符的字符串。大小写不敏感,例如“abc”和“ABC”视为同一个单词。
输入格式:
共一行,为英语文章,长度不超过1000。
输出格式:
共一行,为不同单词的数量。
思路分析
本题可以采用 map 或 set 来记录每个单词出现的次数,也可以通过字符串处理直接进行字符判断,统计单词数。
方法一
首先将文章中所有小写字母转换成大写字母,再遍历每个字符,若是字母,则将其加入当前单词中,若是除字母外的其他字符,则将当前单词加入 set,清空当前单词,同时统计单词总数。
代码如下:
#include <iostream>
#include <set>
using namespace std;
int main()
{
string s;
getline(cin, s);
int n = s.size();
set<string> words;
string cur_word;
for(int i = 0; i < n; i ++)
{
if(s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
if(s[i] >= 'A' && s[i] <= 'Z')
cur_word += s[i];
else
{
if(cur_word.size()) words.insert(cur_word);
cur_word.clear();
}
}
if(cur_word.size()) words.insert(cur_word);
cout << words.size() << endl;
return 0;
}
方法二
使用 STL 的 map 容器,记录每个单词出现的次数,统计不同单词的数量。
代码如下:
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> words;
string s, cur_word;
getline(cin, s);
int n = s.size();
for (int i = 0; i < n; i ++)
{
if(s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
if(s[i] >= 'A' && s[i] <= 'Z')
cur_word += s[i];
else
{
if(cur_word.size()) words[cur_word] ++;
cur_word.clear();
}
}
if(cur_word.size()) words[cur_word] ++;
cout << words.size() << endl;
return 0;
}
C++ 代码
### 回答3:
题目描述:
给定一个以空格为分隔符的字符串,统计其中单词的数目。
分析:
本题其实是一个字符串的基础操作,需要用到的知识点包括字符串的定义和基本操作、循环语句等。
对于本题,我们可以使用一个计数器来记录字符串中单词的数目,每次遇到空格符就将计数器加一。由于题目中要求单词的前后不能有空格,所以我们需要对字符串首位的空格进行处理。最后输出计数器的值即可。
代码实现:
String s = sc.nextLine();//读取输入的字符串
s = s.trim();//去掉字符串首尾的空格
int count = 0;//定义计数器
for(int i = 0; i < s.length(); i++){//循环遍历字符串中的每一个字符
if(s.charAt(i) == ' '){//遇到空格符,计数器加一
count++;
}
}
System.out.println(count+1);//输出计数器的值
注:这里加一的原因是最后一个单词后面没有空格,所以需要再加一。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)