(C语言)字符串大小写无关查找替换

【问题描述】

编写程序将一行英文中指定的字符串替换为另一字符串后输出。注意:查找指定字符串时,大小写无关,而且有可能多次出现。

【输入形式】

先从标准输入中分行输入两个英文字符串(每个字符串不为空,长度不超过20,且字符串中不会出现空格),分别表示被替换的字符串和替换成的字符串,然后在下一行输入一行英文(长度不超过200),行末有回车换行。

【输出形式】

将输入的英文按指定字符串替换后输出到标准输出上,末尾应有一个回车换行。

【样例输入】

th
Hello
This second edition of “The C Programming Language” describes C as defined by the ANSI standard. Modern compilers already support most features of the standard.

【样例输出】

Hellois second edition of “Helloe C Programming Language” describes C as defined by Helloe ANSI standard. Modern compilers already support most features of Helloe standard.

【样例说明】

样例中输入的被替换的字符串为th,由于是大小写无关查找替换,所以下一行英文信息中有四个子串要替换:Th、Th、th和th,分别替换为Hello,其它信息原样输出。

解体思路:将一行英文依次输出,如果不符合替换条件原样输出,如果符合替换条件则输出替换后的。省去了每一处替换时要考虑总字符串长增加还是减小,再进行后面字符整体移动的复杂步骤。

#include<stdio.h>
#include<string.h>
int main()
{
	char a[20], b[20], str[200];//a为要查找的字符,b为替换后的 
	gets(a),gets(b),gets(str);
	int num1 = strlen(a);//统计字符串的长度 
	int num3 = strlen(str);
	int i, j, flag=1;
	for (i = 0; i < num3; i++)
	{
		if ((str[i] != a[0])&&(str[i] != a[0]-32)&&(str[i] !=a[0]+32))//字符串的大小写无关查找  
			printf("%c", str[i]);//若不是,按照原来的输出 
		else{	
			flag=1; 
			for (j = 0; j < num1; j++)//对比a与str中的一段是否完全相同 
			{
				if ((str[i+j] != a[j])&&(str[i+j] != a[j]+32)&&(str[i+j] != a[j]-32))
				{
					flag = 0;
					break;
				}
			}
			if (flag)//如果相同,则输出b,完成替换 
			{
				printf("%s",b);
				i = i + num1;
				i--;
			}
			else//如果不相同,按照原来的输出 
				printf("%c", str[i]);
		}
	}
	return 0;
}
  • 16
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
### 回答1: C语言常用的字符串替换函数是`str_replace`,它可以在一个字符串查找替换指定的子字符串。下面我将简单地模拟实现一个`str_replace`函数。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> char* str_replace(const char* str, const char* search, const char* replace) { char* result; int search_len = strlen(search); int replace_len = strlen(replace); int count = 0; // 统计需要替换的次数 const char* s = str; while ((s = strstr(s, search)) != NULL) { count++; s += search_len; } // 计算替换后的字符串长度 int result_len = strlen(str) + count * (replace_len - search_len); result = (char*)malloc(result_len + 1); // 开始替换 char* p = result; s = str; while ((s = strstr(s, search)) != NULL) { int n = s - str; strncpy(p, str, n); p += n; strncpy(p, replace, replace_len); p += replace_len; str = s + search_len; } strcpy(p, str); return result; } int main(void) { const char* str = "Hello, world!"; const char* search = "world"; const char* replace = "everyone"; char* result = str_replace(str, search, replace); printf("替换前: %s\n", str); printf("替换后: %s\n", result); free(result); return 0; } ``` 上述代码实现了一个简单的`str_replace`函数。它首先统计了需要替换的次数,并根据替换的次数计算了替换后的字符串长度。然后,它分配了足够的内存空间来存储替换后的字符串,并进行了替换操作。最后,它返回了替换后的字符串。在`main`函数,我们可以看到如何使用这个函数来替换一个字符串的子字符串。 需要注意的是,本示例只是对`str_replace`函数的简单模拟实现,实际使用还需要处理更多的边界情况和错误处理。 ### 回答2: C语言没有内置的字符串替换函数,但我们可以通过模拟实现一个字符串替换函数。 首先,我们可以定义一个函数,该函数接受三个参数:源字符串、待替换的子字符串替换后的子字符串。函数的返回值是替换完成后的字符串。 接下来,我们可以使用循环来遍历源字符串。在每次循环,比较源字符串是否存在待替换的子字符串。如果存在,我们就将替换后的子字符串复制到新的字符串。如果不存在,我们将源字符串的当前字符复制到新的字符串。 最后,返回新的字符串即可。 下面是一个简单的模拟实现示例: ```c #include <stdio.h> #include <string.h> char* stringReplace(char* source, const char* search, const char* replace) { char* result; int i, j, sourceLen, searchLen, replaceLen, count; sourceLen = strlen(source); searchLen = strlen(search); replaceLen = strlen(replace); count = 0; for (i = 0; i < sourceLen; i++) { if (strstr(&source[i], search) == &source[i]) { count++; i += searchLen - 1; } } result = (char*)malloc(sourceLen + count * (replaceLen - searchLen) + 1); i = 0; j = 0; while (source[i]) { if (strstr(&source[i], search) == &source[i]) { strcpy(&result[j], replace); j += replaceLen; i += searchLen; } else result[j++] = source[i++]; } result[j] = '\0'; return result; } int main() { char source[] = "Hello, World!"; const char search[] = "World"; const char replace[] = "C Language"; char* result = stringReplace(source, search, replace); printf("替换后的字符串: %s\n", result); free(result); return 0; } ``` 这是一个简单的模拟实现,实际上字符串替换还涉及到更多复杂的情况和细节,比如大小写敏感、替换次数限制等等。需要根据实际需求进行更进一步的完善。 ### 回答3: C语言字符串替换函数模拟实现的方法有很多,以下是一种可能的实现方式: ```c #include <stdio.h> #include <string.h> void str_replace(char *str, const char *find, const char *replace) { int find_len = strlen(find); int replace_len = strlen(replace); int str_len = strlen(str); char result[100]; int result_len = 0; int i = 0; while (i < str_len) { if (strncmp(&str[i], find, find_len) == 0) { // 找到需要替换字符串 strncpy(&result[result_len], replace, replace_len); result_len += replace_len; // 跳过被替换的部分 i += find_len; } else { // 将原字符串的字符复制到结果字符串 result[result_len] = str[i]; result_len++; i++; } } // 将新的结果字符串复制回原字符串 strncpy(str, result, result_len); str[result_len] = '\0'; } int main() { char str[100] = "Hello, World!"; char find[10] = "World"; char replace[10] = "Alice"; printf("Before replace: %s\n", str); str_replace(str, find, replace); printf("After replace: %s\n", str); } ``` 这个函数的思路是首先计算出原字符串、需要查找字符串以及替换字符串的长度。然后以原字符串为基础,通过遍历每个字符的方式,查找需要替换字符串,然后将替换字符串复制到结果字符串,同时跳过原字符串已经被替换的部分。最后将结果字符串复制回原字符串,完成字符串替换。在主函数,我们可以看到使用这个函数对原始字符串的特定字符串进行了替换操作。运行程序后,可以输出替换后的字符串

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值