任务要求 1. 输入英文句子为 str 类型,输出为 bool 类型; 2. 如果句子满足规则要求,程序返回结果为 True;否则返回 False。 测试用例 输入:'I love Python' 输出:True 输入:'python love me' 输出:False 输入: 'JUST DO IT' 输出:True 输入: 'I come from HK' 输出:True 输入: 'Machinelearning is so hot' 输出:False
时间: 2024-01-06 17:04:34 浏览: 34
可以使用正则表达式来判断句子是否符合规则,下面是一个实现:
```python
import re
def check_sentence(sentence):
pattern = r'^[A-Z][a-z\s]*[\.\?!]$'
return bool(re.match(pattern, sentence))
# 测试
print(check_sentence('I love Python')) # True
print(check_sentence('python love me')) # False
print(check_sentence('JUST DO IT')) # True
print(check_sentence('I come from HK')) # True
print(check_sentence('Machinelearning is so hot')) # False
```
正则表达式 `^[A-Z][a-z\s]*[\.\?!]$` 的含义是:
- `^` 表示匹配字符串的开始;
- `[A-Z]` 表示匹配一个大写字母;
- `[a-z\s]*` 表示匹配零个或多个小写字母或空格;
- `[\.\?!]` 表示匹配一个句号、问号或感叹号;
- `$` 表示匹配字符串的结束。
相关问题
用指针完成以下任务:1.判断输入字符是否为回文,若是,输出YES。2.实现strcpy功能
1. 判断回文
```c++
#include <iostream>
#include <cstring>
using namespace std;
bool isPalindrome(char* str) {
int len = strlen(str);
char* left = str;
char* right = str + len - 1;
while (left < right) {
if (*left != *right) {
return false;
}
left++;
right--;
}
return true;
}
int main() {
char str[100];
cin >> str;
if (isPalindrome(str)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
```
2. 实现strcpy功能
```c++
#include <iostream>
#include <cstring>
using namespace std;
char* my_strcpy(char* dest, const char* src) {
char* ret = dest;
while ((*dest++ = *src++) != '\0');
return ret;
}
int main() {
char src[100] = "hello world";
char dest[100];
my_strcpy(dest, src);
cout << dest << endl;
return 0;
}
```
python中datafarm类型转化为str类型
在Python中,将任何数据类型转换为字符串类型都可以使用`str()`函数,例如:
```python
num = 123 # int类型
float_num = 3.14 # float类型
bool_val = True # bool类型
list_val = [1, 2, 3] # list类型
dict_val = {'a': 1, 'b': 2} # dict类型
str_num = str(num)
str_float_num = str(float_num)
str_bool_val = str(bool_val)
str_list_val = str(list_val)
str_dict_val = str(dict_val)
print(type(str_num)) # 输出<class 'str'>
print(type(str_float_num)) # 输出<class 'str'>
print(type(str_bool_val)) # 输出<class 'str'>
print(type(str_list_val)) # 输出<class 'str'>
print(type(str_dict_val)) # 输出<class 'str'>
```
在上述代码中,使用`str()`函数将不同类型的变量转换为字符串类型,并使用`type()`函数检查转换后的类型。
相关推荐
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)