10个星号字符的string串中有一个不是星号,找出该字符的位置,保存在y单元, ******R***。
时间: 2024-06-25 18:00:26 浏览: 5
在这个字符串 "******R***" 中,你的目标是找到第一个非星号字符 'R' 的位置。我们可以遍历整个字符串,当遇到的第一个非星号字符被找到时,就将其索引赋值给变量 `y`。这里是解决方案:
1. 初始化一个变量 `y` 为 -1,表示未找到非星号字符。
2. 遍历字符串,从左到右,对于每个字符:
a. 如果字符是星号 '*':
- 继续遍历下一个字符。
b. 如果字符不是星号:
- 更新 `y` 为当前字符的索引(在字符串中,索引从0开始)。
- 结束遍历,因为一旦找到非星号字符,就不需要继续检查后面的星号。
3. 返回变量 `y`。
根据这个描述,变量 `y` 的值应该是6,因为 'R' 在字符串中的位置是第7个字符,但字符串的索引从0开始计数。
相关问题
10个星号字符的string串中有一个不是星号,找出该字符的位置,保存在y单元, ******R***完整代码
可以使用C++的string库和for循环来实现该功能,代码如下:
```
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "******R***";
int len = str.length();
int y = -1;
for (int i = 0; i < len; i++) {
if (str[i] != '*') {
y = i;
break;
}
}
cout << "y的位置为:" << y << endl;
return 0;
}
```
解释一下上述代码:首先定义一个string类型的变量str,并将其初始化为"******R***",然后获取字符串的长度len,接着定义一个变量y,并初始化为-1,用for循环遍历字符串,如果当前字符不是星号,则将该字符的位置赋值给y,并跳出循环。最后输出y的值。
汇编语言 10个星号字符的string串中有一个不是星号,找出该字符的位置,保存在y单元, ******R***完整代码
汇编语言是一种低级别的计算机语言,用于控制计算机硬件执行指令。汇编语言通常使用助记符来代替二进制指令,以便更容易地理解和编写。
以下是查找10个星号字符的string串中第一个不是星号的字符位置的汇编代码:
```
section .data
str db "**********R***", 0 ;定义字符串,末尾需要加0
section .text
global _start
_start:
mov ecx, 0 ;初始化ecx为0
mov esi, str ;将str字符串的地址存入esi寄存器
mov al, '*' ;将'*'字符存入al寄存器
repeat:
cmp byte [esi], al ;比较esi指向的字节是否等于'*'
jne found ;如果不是,跳转到found标签
inc ecx ;如果是,ecx加1
inc esi ;esi指向下一个字节
cmp byte [esi], 0 ;比较esi指向的字节是否为字符串末尾
jne repeat ;如果不是,继续循环
not_found:
mov y, -1 ;如果找不到,将-1存入y单元
jmp exit ;跳转到exit标签
found:
mov y, ecx ;将ecx存入y单元
exit:
mov eax, 1 ;退出程序
xor ebx, ebx
int 0x80
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)