c++1.编写程序,对于任意两个串:主串和子串,找出子串在主串中的位置。
时间: 2024-06-19 18:03:02 浏览: 31
您好!针对您的问题,可以使用C++中的字符串处理函数进行实现。下面是一段示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string main_str, sub_str;
cout << "请输入主串:";
getline(cin, main_str); // 获取整行输入
cout << "请输入子串:";
getline(cin, sub_str);
size_t pos = main_str.find(sub_str); // 在主串中查找子串
if (pos != string::npos) { // 如果找到
cout << "子串在主串中的位置为:" << pos << endl;
} else { // 如果未找到
cout << "未找到子串!" << endl;
}
return 0;
}
```
在以上代码中,我们使用了`string`类型的变量`main_str`和`sub_str`来分别存储输入的主串和子串,然后使用`find`函数在主串中查找子串,并返回子串在主串中的位置。如果查找成功,则输出位置;否则输出未找到提示。
相关问题
c++1.编写程序,对于任意两个串:主串和子串,找出子串在主串中的位置。 如:主串为a b a b c a b,子串为abc,找输出其位置为3;若子串为abd,则提示子串在主串中不存在。
可以使用暴力匹配算法,逐一比较主串和子串的每一个字符,找到第一个匹配的字符后,再一一比较后面的字符是否相同,若全部匹配成功,则说明子串在主串中出现过。
下面是具体的实现过程:
```c++
#include <iostream>
#include <string>
using namespace std;
int findSubstr(string str, string substr) {
int str_len = str.size();
int substr_len = substr.size();
for (int i = 0; i <= str_len - substr_len; i++) {
int j = 0;
while (j < substr_len && str[i+j] == substr[j]) {
j++;
}
if (j == substr_len) {
return i;
}
}
return -1;
}
int main() {
string str = "ababcab";
string substr = "abc";
int pos = findSubstr(str, substr);
if (pos == -1) {
cout << "子串在主串中不存在" << endl;
} else {
cout << "子串在主串中的位置为:" << pos << endl;
}
return 0;
}
```
如果子串存在于主串中,输出结果为:
```
子串在主串中的位置为:2
```
如果子串不存在于主串中,输出结果为:
```
子串在主串中不存在
```
请使用C语言编写一个程序:找出两个字符串中的最大公共子串
当然可以!下面是一个使用C语言编写的程序,用于找出两个字符串中的最大公共子串:
```c
#include <stdio.h>
#include <string.h>
void findLCS(char *str1, char *str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
int maxLen = 0; // 最大公共子串的长度
int endIdx = 0; // 最大公共子串的结束索引
// 创建一个二维数组用于存储最长公共子串的长度
int dp[len1 + 1][len2 + 1];
// 初始化dp数组
for (int i = 0; i <= len1; i++) {
for (int j = 0; j <= len2; j++) {
if (i == 0 || j == 0)
dp[i][j] = 0;
else if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
if (dp[i][j] > maxLen) {
maxLen = dp[i][j];
endIdx = i - 1;
}
} else {
dp[i][j] = 0;
}
}
}
// 打印最大公共子串
if (maxLen == 0) {
printf("No common substring found.\n");
} else {
printf("Longest common substring: ");
for (int i = endIdx - maxLen + 1; i <= endIdx; i++) {
printf("%c", str1[i]);
}
printf("\n");
}
}
int main() {
char str1[100];
char str2[100];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // 移除输入字符串中的换行符
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0'; // 移除输入字符串中的换行符
findLCS(str1, str2);
return 0;
}
```
你可以使用上述代码来找出两个字符串中的最大公共子串。程序会要求你输入两个字符串,然后打印出最大公共子串。请注意,程序中使用了一个二维数组 `dp` 来存储最长公共子串的长度,使用动态规划的思想进行求解。
相关推荐
![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)