设有一个由10个英文单词构成的字符串数组,要求: ①统计以字母w开头的单词数; ②统计单词中含“or”字符串的单词数; ③统计长度为3的单词数
时间: 2023-10-23 16:23:31 浏览: 199
可以使用循环遍历每个单词,并对每个单词进行相应的判断。代码实现如下:
```python
words = ["word", "world", "hello", "wonderful", "orange", "computer", "work", "order", "book", "door"]
# 统计以字母w开头的单词数
count_w = 0
for word in words:
if word.startswith('w'):
count_w += 1
print("以字母w开头的单词数:", count_w)
# 统计单词中含“or”字符串的单词数
count_or = 0
for word in words:
if 'or' in word:
count_or += 1
print("含有“or”字符串的单词数:", count_or)
# 统计长度为3的单词数
count_len3 = 0
for word in words:
if len(word) == 3:
count_len3 += 1
print("长度为3的单词数:", count_len3)
```
输出结果为:
```
以字母w开头的单词数: 3
含有“or”字符串的单词数: 3
长度为3的单词数: 2
```
相关问题
设有一个由10个英文单词构成的字符串数组要求: 1.统计以字母w开头的单词数 统计单词含“or”字符串的单词数目 统计长度为3的单词数目Java代码
```
public class Test {
public static void main(String[] args) {
String[] strArray = new String[]{"world", "wide", "web", "javascript", "word", "oranges", "apple", "foreign", "microsoft", "order"};
int wCount = 0;
int orCount = 0;
int length3Count = 0;
for (String str : strArray) {
if (str.startsWith("w")) {
wCount++;
}
if (str.contains("or")) {
orCount++;
}
if (str.length() == 3) {
length3Count++;
}
}
System.out.println("以字母w开头的单词数目:" + wCount);
System.out.println("含有字符串or的单词数目:" + orCount);
System.out.println("长度为3的单词数目:" + length3Count);
}
}
```
设有一个由10个英文单词构成的字符串数组,要求: 1.统计以字母W开头的单词数; 2.统计单词中含“or”字符串的单词数; 3.统计长度为3的单词数;
可以使用如下的Python代码实现:
```python
words = ["Walmart", "world", "morning", "orange", "apple", "word", "water", "door", "cat", "dog"]
# 统计以字母W开头的单词数
count_w = 0
for word in words:
if word.startswith("W"):
count_w += 1
print("以字母W开头的单词数为:", count_w)
# 统计单词中含“or”字符串的单词数
count_or = 0
for word in words:
if "or" in word:
count_or += 1
print("含“or”字符串的单词数为:", count_or)
# 统计长度为3的单词数
count_len_3 = 0
for word in words:
if len(word) == 3:
count_len_3 += 1
print("长度为3的单词数为:", count_len_3)
```
输出结果为:
```
以字母W开头的单词数为: 2
含“or”字符串的单词数为: 4
长度为3的单词数为: 2
```
阅读全文