如何将字符串1中与字符串2相同的字符串删除
时间: 2024-06-12 15:10:45 浏览: 10
要将字符串1中与字符串2相同的字符串删除,可以按照以下步骤进行操作:
1. 定义一个函数,函数名为delete_string,函数参数为两个字符数组str1和str2。
2. 在函数中使用两个for循环,分别遍历str1和str2中的每个字符。
3. 如果str1中的某个字符与str2中的某个字符相同,则将该字符从str1中删除。
4. 删除字符的方法可以使用strcpy函数和指针的方法实现。
5. 最后输出删除后的字符串str1。
以下是示例代码:
void delete_string(char str1[], char str2[]) {
int i, j, k;
for (i = 0; str2[i] != '\0'; i++) {
for (j = 0; str1[j] != '\0'; j++) {
if (str1[j] == str2[i]) {
for (k = j; str1[k] != '\0'; k++) {
str1[k] = str1[k + 1];
}
j--;
}
}
}
printf("删除后的字符串为:%s\n", str1);
}
相关问题
C语言在字符串1中删除字符串2
可以使用C语言的字符串函数`strstr()`和`strcpy()`来实现删除字符串2的功能。具体实现如下:
```c
#include<stdio.h>
#include<string.h>
void deleteSubstring(char str1[], char str2[]) {
int len1 = strlen(str1);
int len2 = strlen(str2);
char *ptr1, *ptr2, *ptr3;
ptr1 = str1;
while ((ptr2 = strstr(ptr1, str2)) != NULL) {
ptr3 = ptr2 + len2;
while (*ptr3 != '\0') {
*ptr2 = *ptr3;
ptr2++;
ptr3++;
}
*ptr2 = '\0';
ptr1 = ptr2;
}
}
int main() {
char str1[100], str2[20];
printf("Enter string 1: ");
gets(str1);
printf("Enter string 2: ");
gets(str2);
deleteSubstring(str1, str2);
printf("String after deletion: %s", str1);
return 0;
}
```
在上述代码中,我们首先计算了字符串1和字符串2的长度。然后,我们使用`strstr()`函数来查找字符串2在字符串1中的位置。如果找到了,我们使用`strcpy()`函数将字符串1中从字符串2开始的所有字符向前移动,直到字符串1结束,然后将字符串1的末尾设置为字符串2的末尾。
请注意,此代码只会删除字符串2的第一个匹配项。如果字符串2在字符串1中出现多次,则需要在循环中使用`strstr()`函数并进行多次删除。
用c语言在字符串1中删除字符串2
以下是用C语言实现从字符串1中删除字符串2的示例代码:
```c
#include <stdio.h>
#include <string.h>
void delete_substr(char *str1, char *str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
int i, j, k;
for (i = 0; i <= len1 - len2; ++i) {
k = i;
// 查找是否包含字符串2
for (j = 0; j < len2; ++j) {
if (str1[k] == str2[j]) {
++k;
} else {
break;
}
}
// 找到字符串2,删除并移动指针
if (j == len2) {
memmove(&str1[i], &str1[i + len2], len1 - i - len2 + 1);
i -= len2;
len1 -= len2;
}
}
}
int main() {
char str1[100] = "hello world";
char str2[10] = "llo";
delete_substr(str1, str2);
printf("After deleting: %s\n", str1);
return 0;
}
```
以上代码中,`delete_substr` 函数用于删除字符串1中的字符串2。首先计算字符串1和字符串2的长度,然后遍历字符串1中每一个可能的起始位置,查找是否包含字符串2。如果找到字符串2,就通过 `memmove` 函数将字符串1中的字符串2删除,并移动后面的字符。最后输出删除后的字符串1。
相关推荐
![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)