删除字符串重复字母c语言,C语言去除相邻重复字符函数的实现方法

C语言去除相邻重复字符函数的实现方法

字符去重函数

功能:去重字符串相邻重复的字符,不相邻的不用去重

参数:

arg1 -- 输入字符串

arg2 -- 字符串开始位置

arg3 -- 字符串结束位置

要求:

输入参数为arg1时, 对这个字符串去重

输入参数为arg1,arg2时, 从arg2位置到字符串结束,去重

输入参数为arg1,arg2,arg3时,从arg2到arg3位置,去重

src/include/catalog/pg_proc.h

DATA(insert OID = 6669 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 25 "25" _null_ _null_ _null_ _null_ _null_ remove_dup_char_arg1 _null_ _null_ _null_ ));

DESCR("Remove duplicate characters.");

DATA(insert OID = 6670 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 25 "25 23" _null_ _null_ _null_ _null_ _null_ remove_dup_char_arg2 _null_ _null_ _null_ ));

DESCR("Remove duplicate characters.");

DATA(insert OID = 6671 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 3 0 25 "25 23 23" _null_ _null_ _null_ _null_ _null_ remove_dup_char_arg3 _null_ _null_ _null_ ));

DESCR("Remove duplicate characters.");

src/backend/utils/adt/myfuncs.c

/*

* Remove duplicate characters

* author:young

*/

Datum

remove_dup_char_arg1 (PG_FUNCTION_ARGS)

{

int n = 0;

text *arg0 = PG_GETARG_TEXT_P(0);

char *str = text_to_cstring(arg0);

n = strlen(str);

remove_dup(str, 0, n);

PG_RETURN_TEXT_P(cstring_to_text(str));

}

Datum

remove_dup_char_arg2 (PG_FUNCTION_ARGS)

{

int n = 0;

text *arg0 = PG_GETARG_TEXT_P(0);

int32 arg1 = PG_GETARG_INT32(1);

char *str = text_to_cstring(arg0);

n = strlen(str);

if (!(1 <= arg1 && arg1 <= n))

{

ereport(ERROR,

(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),

errmsg("out of range")));

}

remove_dup(str, arg1 - 1, n);

PG_RETURN_TEXT_P(cstring_to_text(str));

}

Datum

remove_dup_char_arg3 (PG_FUNCTION_ARGS)

{

int n = 0;

text *arg0 = PG_GETARG_TEXT_P(0);

int32 arg1 = PG_GETARG_INT32(1);

int32 arg2 = PG_GETARG_INT32(2);

char *str = text_to_cstring(arg0);

n = strlen(str);

if (!(1 <= arg1 && arg1 <= arg2 && arg2 <= n))

{

ereport(ERROR,

(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),

errmsg("out of range")));

}

remove_dup(str, arg1 - 1, arg2 - 1);

PG_RETURN_TEXT_P(cstring_to_text(str));

}

void

remove_dup(char *str, int start, int end)

{

int i = start, k = start;

for (i = start; i <= end; i++)

{

if (str[i + 1] && str[i + 1] == str[i] && i + 1 <= end)

{

k++;

}

else

{

str[i-k] = str[i];

}

}

str[i-k] = '\0';

}

比较繁琐,再做一下修改,三个函数放到一个中

src/include/catalog/pg_proc.h

DATA(insert OID = 6669 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 25 "25" _null_ _null_ _null_ _null_ _null_ remove_dup_char _null_ _null_ _null_ ));

DESCR("Remove duplicate characters.");

DATA(insert OID = 6670 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 25 "25 23" _null_ _null_ _null_ _null_ _null_ remove_dup_char _null_ _null_ _null_ ));

DESCR("Remove duplicate characters.");

DATA(insert OID = 6671 ( remove_dup_char PGNSP PGUID 12 1 0 0 0 f f f f t f i 3 0 25 "25 23 23" _null_ _null_ _null_ _null_ _null_ remove_dup_char _null_ _null_ _null_ ));

DESCR("Remove duplicate characters.");

src/backend/utils/adt/myfuncs.c

添加定义:

#define PG_GETARG_IF_EXISTS(n, type, defval) \

((PG_NARGS() > (n) && !PG_ARGISNULL(n)) ? PG_GETARG_##type(n) : (defval))

修改方法:

/*

* Remove duplicate characters

* author:yangjie

*/

Datum

remove_dup_char (PG_FUNCTION_ARGS)

{

text *arg0 = PG_GETARG_IF_EXISTS(0, TEXT_P, NULL);

int32 arg1 = PG_GETARG_IF_EXISTS(1, INT32, 0);

int32 arg2 = PG_GETARG_IF_EXISTS(2, INT32, 0);

int n = 0;

char *str = text_to_cstring(arg0);

n = strlen(str);

if(PG_NARGS() == 1)

{

remove_dup(str, 0, n);

}

if(PG_NARGS() == 2)

{

if (!(1 <= arg1 && arg1 <= n))

{

ereport(ERROR,

(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),

errmsg("out of range")));

}

remove_dup(str, arg1 - 1, n);

}

if(PG_NARGS() == 3)

{

if (!(1 <= arg1 && arg1 <= arg2 && arg2 <= n))

{

ereport(ERROR,

(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),

errmsg("out of range")));

}

remove_dup(str, arg1 - 1, arg2 - 1);

}

PG_RETURN_TEXT_P(cstring_to_text(str));

}

再修改一下,如果有输入参数就用 没有就用默认值  最后再去重处理减少代码重用

/*

* Remove duplicate characters

* author:yangjie

*/

Datum

remove_dup_char (PG_FUNCTION_ARGS)

{

text *arg0 = PG_GETARG_IF_EXISTS(0, TEXT_P, NULL);

int n = 0;

char *str = text_to_cstring(arg0);

n = strlen(str);

int32 arg1 = PG_GETARG_IF_EXISTS(1, INT32, 0);

int32 arg2 = PG_GETARG_IF_EXISTS(2, INT32, n);

if (!(1 <= arg1 && arg1 <= n))

{

ereport(ERROR,

(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),

errmsg("1 <= arg1 && arg1 <= n")));

}

if (!(1 <= arg1 && arg1 <= arg2 && arg2 <= n))

{

ereport(ERROR,

(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),

errmsg("1 <= arg1 && arg1 <= arg2 && arg2 <= n")));

}

remove_dup(str, arg1, arg2);

PG_RETURN_TEXT_P(cstring_to_text(str));

}

void

remove_dup(char *str, int start, int end)

{

int i = start -1, k = start - 1;

for (i = start - 1; i <= end - 1; i++)

{

if (str[i + 1] && str[i + 1] == str[i] && i + 1 <= end - 1)

{

k++;

}

else

{

str[i-k] = str[i];

}

}

str[i-k] = '\0';

}

以上就是C语言去除相邻重复字符函数的实现方法,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用指针数组来实现字符串按字典顺序排序,具体步骤如下: 1. 定义指针数组,每个指针指向一个字符串; 2. 使用双重循环遍历指针数组,比较相邻两个字符串的大小,如果前一个字符串大于后一个字符串,则进行指针交换; 3. 循环完成后,指针数组中的字符串就按字典顺序排序好了。 下面是示例代码: ```c #include <stdio.h> #include <string.h> #define MAX_LEN 100 // 定义字符串最大长度 #define MAX_NUM 100 // 定义字符串数量 void bubble_sort(char *arr[], int n); int main() { char str[MAX_NUM][MAX_LEN]; // 定义二维字符数组 char *p[MAX_NUM]; // 定义指针数组 int n, i; printf("请输入字符串的数量:"); scanf("%d", &n); printf("请输入 %d 个字符串(每行一个):\n", n); for (i = 0; i < n; i++) { scanf("%s", str[i]); p[i] = str[i]; // 每个指针指向一个字符串 } bubble_sort(p, n); printf("按字典顺序排序后的字符串为:\n"); for (i = 0; i < n; i++) { printf("%s\n", p[i]); } return 0; } void bubble_sort(char *arr[], int n) { int i, j; char *temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (strcmp(arr[j], arr[j + 1]) > 0) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } ``` 注意,在这个例子中,我们使用了指针数组,每个指针指向一个字符串。在 `bubble_sort()` 函数中,我们比较的是指针所指向的字符串的大小,而不是字符串本身的大小。在交换指针时,我们只是交换指针本身的位置,而不是交换指针所指向的字符串的位置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值