PAT 1077 字符串处理

题目

给出几个字符串,求出他们的公共后缀
Sample Input 1:

3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

Sample Output 1:

nyan~

Sample Input 2:

3
Itai!
Ninjinnwaiyada T_T
T_T

Sample Output 2:

nai

代码和思路

  1. 要求最长后缀,但如果从后面开始数的话,因为每个字符串长度都不同,所以无法统一进行循环,所以先逆序
  2. 要记录字符串中的最短长度,以防循环的时候越界了
  3. 碰到不相等的就直接跳出循环,但是计数器要注意++的时间点,是所有字符串都相等的时候才能++
  4. PAT中不允许使用gets,可以改成string和用getline读入
#include<cstdio>
#include<string.h>
int n, minlen = 256, ans = 0;
char a[100][256];

int main() {
	scanf_s("%d", &n);
	getchar();
	for (int i = 0; i < n; i++) {
		gets_s(a[i]);
		int len = strlen(a[i]);
		if (len < minlen) minlen = len;
		for (int j = 0; j < len / 2; j++) {
			char temp = a[i][j];
			a[i][j] = a[i][len - j - 1];
			a[i][len - j - 1] = temp;
		}
	}
	for (int i = 0; i < minlen; i++) {
		char c = a[0][i];
		bool same = true;
		for (int j = 1; j < n; j++) {
			if (c != a[j][i]) {
				same = false;
				break;
			}
		}
		if (same) ans++;
		else break;
	}
	if (ans) {
		for (int i = ans - 1; i >= 0; i--) {
			printf("%c", a[0][i]);
		}
	}
	else {
		printf("nai");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要替换一个字符串中的某个子串,可以使用C语言中的字符串函数strstr()和strcpy()。具体步骤如下: 1. 使用strstr()函数找到需要替换的子串在原字符串中的位置。 2. 使用strcpy()函数将替换后的新子串复制到一个新的字符串中。 3. 将新字符串和原字符串合并起来,即完成了字符串替换的操作。 下面是一个示例代码,可以实现将字符串中的某个子串替换成另一个字符串: ```c #include <stdio.h> #include <string.h> void replace_str(char *str, char *old, char *new) { char *pos = strstr(str, old); if (pos != NULL) { int len_old = strlen(old); int len_new = strlen(new); if (len_new > len_old) { memmove(pos + len_new, pos + len_old, strlen(pos + len_old) + 1); } memcpy(pos, new, len_new); } } int main() { char str[100] = "pta is good, pta is great!"; replace_str(str, "pta", "PAT"); printf("%s\n", str); return 0; } ``` 输出结果为: ``` PAT is good, PAT is great! ``` ### 回答2: c语言中的字符串替换可以通过使用字符串处理函数和循环来实现。下面是一个用C语言实现字符串替换的示例代码: ```c #include <stdio.h> #include <string.h> void replaceString(char *str, const char *find, const char *replace) { int findLen = strlen(find); int replaceLen = strlen(replace); int strLen = strlen(str); int newSize = strLen; int replaceCount = 0; // 计算新字符串的长度,以便分配足够的内存 for (int i = 0; i < strLen; i++) { if (strstr(&str[i], find) == &str[i]) { newSize -= findLen; newSize += replaceLen; replaceCount++; i += findLen - 1; } } // 分配内存 char *result = (char *)malloc(newSize + 1); memset(result, 0, newSize + 1); int j = 0; for (int i = 0; i < strLen; i++) { if (strstr(&str[i], find) == &str[i]) { strncpy(&result[j], replace, replaceLen); j += replaceLen; i += findLen - 1; } else { result[j++] = str[i]; } } printf("替换前的字符串: %s\n", str); printf("要替换的字符串: %s\n", find); printf("替换后的字符串: %s\n", result); printf("共替换了 %d 处\n", replaceCount); free(result); } int main() { char str[100] = "pta是一个pta网站"; const char find[] = "pta"; const char replace[] = "PAT"; replaceString(str, find, replace); return 0; } ``` 该示例代码中定义了一个`replaceString`函数,并在`main`函数中调用。在`replaceString`函数中,我们首先获取要替换的字符串的长度和原始字符串的长度,然后计算出替换后的字符串的长度。利用循环遍历原始字符串,查找要替换的子字符串,如果找到,则将替换字符串复制到新字符串中;如果未找到,则将原始字符串中的字符复制到新字符串中。 在`main`函数中,我们定义了一个原始字符串`str`,以及要查找和替换的字符串`find`和`replace`。然后调用`replaceString`函数进行字符串替换,并输出替换前的字符串、要替换的字符串、替换后的字符串和替换的次数。 以上就是一个简单的C语言字符串替换的例子。通过使用字符串处理函数和循环,我们可以在C语言中实现字符串替换。 ### 回答3: 在C语言中,我们可以使用字符串函数来实现字符串的替换操作。下面是一个简单的示例代码: ```c #include <stdio.h> #include <string.h> void replaceString(char *str, const char *old_substr, const char *new_substr) { char *pos = strstr(str, old_substr); // 在str中找到第一个出现的old_substr的位置 while (pos != NULL) { size_t old_len = strlen(old_substr); // 获取old_substr的长度 size_t new_len = strlen(new_substr); // 获取new_substr的长度 size_t tail_len = strlen(pos + old_len); // 获取原字符串剩余部分的长度 if (new_len > old_len) { memmove(pos + new_len, pos + old_len, tail_len + 1); // 将原字符串剩余部分向后移动 } else if (new_len < old_len) { memmove(pos + new_len, pos + old_len, tail_len + 1); // 将原字符串剩余部分向前移动 } strncpy(pos, new_substr, new_len); // 将new_substr的内容拷贝到原字符串中 pos = strstr(pos + new_len, old_substr); // 在修改后的字符串中继续查找下一个old_substr的位置 } } int main() { char str[100] = "pta is a good platform. pta helps me improve coding skills."; char old_substr[] = "pta"; char new_substr[] = "PTA"; replaceString(str, old_substr, new_substr); printf("%s\n", str); // 输出修改后的字符串 return 0; } ``` 上述代码中的`replaceString`函数可以将原字符串中的所有`old_substr`替换为`new_substr`,并且保持原字符串的长度不变。在`main`函数中,我们定义了一个字符串`str`,并将其中的所有"pta"替换为"PTA"。最后,我们将修改后的字符串输出到控制台上。 以上代码仅为示例,实际替换操作可能需要根据具体需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值