编写程序,我用户输入一个字符串,输出用户输入的最后一个字符
时间: 2024-05-08 09:17:20 浏览: 13
```python
string = input("请输入一个字符串:")
last_char = string[-1]
print("最后一个字符是:", last_char)
```
或者更简洁的写法:
```python
last_char = input("请输入一个字符串:")[-1]
print("最后一个字符是:", last_char)
```
相关问题
c++编写一个程序 接收用户输入的一个字符串
,然后反转字符串并输出。
很高兴回答你的问题!以下是解决方案:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
int len = strlen(str);
for(int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
printf("反转后的字符串为:%s", str);
return 0;
}
你可以将用户输入的字符串保存在一个字符数组中,然后使用一个 for 循环来反转字符串中的字符。具体来说,我们可以交换字符串的第一个字符和最后一个字符,第二个字符和倒数第二个字符,以此类推。最后输出反转后的字符串即可。
希望对你有所帮助!
编写一个小程序,实现输入一个字符串,倒序输出
好的,以下是实现输入一个字符串,倒序输出的小程序的代码:
```
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
int len = strlen(str);
printf("倒序输出的结果为:");
for (int i = len - 1; i >= 0; i--)
{
printf("%c", str[i]);
}
printf("\n");
return 0;
}
```
希望能对您有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![](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)