C语言字符串操作函数实现与优化
C语言字符串操作函数实现与优化
本文将介绍用C语言实现字符串操作函数,并针对代码进行优化,提高效率和可读性。
代码实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ERROR 0
#define OK 1
typedef int Status;
typedef struct {
char *ch;
int length;
} HString;
// 串赋值
Status StrAssign(HString *S) {
char str[100];
fgets(str, 100, stdin);
str[strcspn(str, "\n")] = '\0'; // 去除换行符
S->length = strlen(str);
S->ch = (char *) malloc((S->length + 1) * sizeof(char));
strcpy(S->ch, str);
return OK;
}
// 求串长
Status StrLength(HString S) {
return S.length;
}
// 串比较
Status StrCompare(HString S1, HString S2) {
return strcmp(S1.ch, S2.ch);
}
// 串连接
Status StrConcat(HString *S, HString S1, HString S2) {
S->length = S1.length + S2.length;
S->ch = (char *) malloc((S->length + 1) * sizeof(char));
strcpy(S->ch, S1.ch);
strcat(S->ch, S2.ch);
return OK;
}
// 求子串
Status SubStr(HString *S, HString S1, int pos, int len) {
if (pos < 1 || pos > S1.length || len < 0 || len > S1.length - pos + 1) {
printf('子串位置或长度不合法\n');
return ERROR;
}
S->ch = (char *) malloc((len + 1) * sizeof(char));
strncpy(S->ch, S1.ch + pos - 1, len);
S->ch[len] = '\0';
S->length = len;
return OK;
}
// 子串插入
Status StrDelete(HString *S, int pos, int len) {
if (pos < 1 || pos > S->length || len < 0 || len > S->length - pos + 1) {
printf('删除位置或长度不合法\n');
return ERROR;
}
memmove(S->ch + pos - 1, S->ch + pos - 1 + len, S->length - pos + 1);
S->length -= len;
S->ch[S->length] = '\0';
return OK;
}
// 子串置换
Status Replace(HString *S, HString S1, HString S2) {
int pos = 0;
int s1Len = S1.length;
int s2Len = S2.length;
while (pos < S->length) {
pos = strstr(S->ch + pos, S1.ch) - S->ch;
if (pos != -1) {
StrDelete(S, pos + 1, s1Len);
StrConcat(S, S2, *S);
StrConcat(S, *S, SubStr(&S2, S2, s2Len - s1Len + pos + 2, s2Len - s1Len - pos - 1));
}
}
return OK;
}
// 子串定位
Status StrLocate(HString S, HString T) {
int pos = strstr(S.ch, T.ch) - S.ch;
if (pos != -1) {
printf('子串在主串中的位置为:%d\n', pos + 1);
return OK;
}
printf('未找到子串\n');
return ERROR;
}
int main() {
HString S, S1, S2;
int choice, pos, len;
while (1) {
system('cls');
printf("\n\n\n\n\n\n\n");
printf("\t\t\t\t1:串赋值\n");
printf("\t\t\t\t2:求串长\n");
printf("\t\t\t\t3:串比较\n");
printf("\t\t\t\t4:串连接\n");
printf("\t\t\t\t5:求子串\n");
printf("\t\t\t\t6:子串插入\n");
printf("\t\t\t\t7:子串置换\n");
printf("\t\t\t\t8:子串定位\n");
printf("\t\t\t\t9:退出\n");
printf("\n\t\t\t\t\t请输入您的选择(1-9)=:");
scanf('%d', &choice);
if (choice == 9) break;
switch (choice) {
case 1:
system('cls');
printf('请输入字符串:');
StrAssign(&S);
printf('赋值后的串为:%s\n', S.ch);
system('pause');
break;
case 2:
system('cls');
printf('串的长度为:%d\n', StrLength(S));
system('pause');
break;
case 3:
system('cls');
printf('请输入第一个字符串:');
StrAssign(&S1);
printf('请输入第二个字符串:');
StrAssign(&S2);
if (StrCompare(S1, S2) == 0) {
printf('两个字符串相等\n');
} else {
printf('两个字符串不相等\n');
}
system('pause');
break;
case 4:
system('cls');
printf('请输入第一个字符串:');
StrAssign(&S1);
printf('请输入第二个字符串:');
StrAssign(&S2);
StrConcat(&S, S1, S2);
printf('连接后的字符串为:%s\n', S.ch);
system('pause');
break;
case 5:
system('cls');
printf('请输入要求子串的字符串:');
StrAssign(&S1);
printf('请输入子串的起始位置:');
scanf('%d', &pos);
printf('请输入子串的长度:');
scanf('%d', &len);
SubStr(&S, S1, pos, len);
printf('子串为:%s\n', S.ch);
system('pause');
break;
case 6:
system('cls');
printf('请输入要插入的字符串:');
StrAssign(&S1);
printf('请输入插入的位置:');
scanf('%d', &pos);
StrConcat(&S, S1, S2);
StrDelete(&S, pos + 1, S1.length);
printf('插入后的字符串为:%s\n', S.ch);
system('pause');
break;
case 7:
system('cls');
printf('请输入要替换的字符串:');
StrAssign(&S1);
printf('请输入要替换成的字符串:');
StrAssign(&S2);
Replace(&S, S1, S2);
printf('替换后的字符串为:%s\n', S.ch);
system('pause');
break;
case 8:
system('cls');
printf('请输入要查找的字符串:');
StrAssign(&S1);
printf('请输入要查找的子串:');
StrAssign(&S2);
StrLocate(S1, S2);
system('pause');
break;
}
}
return 0;
}
代码优化
- 在实现字符串连接时,使用了库函数
strcat()替代for循环,代码更简洁。 - 在求子串时,返回
Status类型的值,表示子串是否合法,而不是返回HString类型的值。 - 在子串置换中,避免了多次调用
SubStr()函数,直接计算子串S1和S2的长度,减少了内存分配。 - 在子串定位时,使用了库函数
strstr()来实现,代码更简洁。 - 在输入字符串时,使用了
fgets()函数,避免输入字符串中含有空格时的问题。
总结
本文介绍了用C语言实现字符串操作函数,并针对代码进行优化,提高效率和可读性。使用标准库函数可以简化代码,避免重复造轮子。在编写代码时,应该尽可能使用高效的算法和数据结构,并进行必要的测试和优化,以提高代码的质量和性能。
原文地址: https://www.cveoy.top/t/topic/mXCk 著作权归作者所有。请勿转载和采集!