编写具体函数实现子串位置的定位函数,例如编写串的判等、求串长、求子串等操作函数实现
时间: 2024-05-08 19:19:06 浏览: 9
假设字符串以char*类型存储,以下是具体的函数实现:
1. 串的判等函数:
```c++
bool isEqual(char* str1, char* str2)
{
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0')
{
if (str1[i] != str2[i])
return false;
i++;
}
if (str1[i] == '\0' && str2[i] == '\0')
return true;
else
return false;
}
```
2. 求串长函数:
```c++
int getLength(char* str)
{
int len = 0;
while (str[len] != '\0')
len++;
return len;
}
```
3. 求子串函数:
```c++
int findSubstring(char* str, char* subStr)
{
int strLen = getLength(str);
int subStrLen = getLength(subStr);
if (subStrLen > strLen)
return -1;
for (int i = 0; i < strLen - subStrLen + 1; i++)
{
if (isEqual(str + i, subStr))
return i;
}
return -1;
}
```
以上函数实现了串的判等、求串长、求子串位置等操作。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)