C语言进阶之路(二)----字符串操作常见模型

时间:2023-03-09 08:23:15
C语言进阶之路(二)----字符串操作常见模型

1.while模型

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //求一个字符串中某个子串出现的次数
int getCout(char *str, char *substr, int *count)
{
int rv = ;
char *p = str; int ncout = ;
if (str==NULL || substr== NULL || count==NULL)
{
rv = -;
printf("func getCout()check (str==NULL || substr== NULL || count==NULL) err:%d \n" , rv);
return rv;
}
while (*p != '\0'){
p = strstr(p, substr);
if (p == NULL)
{
break;
}
else
{
ncout++;
p = p + strlen(substr);
} } ;
//通过指针把结果传出来
*count = ncout;
return rv;
} int main()
{
int ret = ;
char *p = "abcd1111abcd222abcd3333";
char *subp = "abcd";
int ncout = ; ret = getCout(p, subp, &ncout);
if (ret != )
{
printf("func getCout() err:%d \n", ret);
return ;
}
printf("coutn = %d \n", ncout);
return ;
}

2.两头堵模型:两种写法

//求去掉两边空格之后的字符串长度,指针作为形参传入,将结果赋值给指针指向的内存
int trimSpaceStr01(char *p, int *mycount)
{
int ret = ; int ncount = ;
int i= , j;
j = strlen(p) - ; while (isspace(p[i]) && p[i] != '\0')
{
i++;
} while (isspace(p[j]) && j>)
{
j--;
} ncount = j - i + ;
*mycount = ncount;
return ret;
} //求去掉两边空格之后的字符串,将指针作为形参传入,将结果赋值给形参指向的内存空间
int trimSpaceStr2(char *p, char *buf)
{
int ret = ; int ncount = ;
int i, j;
i = ;
j = strlen(p) - ; while (isspace(p[i]) && p[i] != '\0')
{
i++;
} while (isspace(p[j]) && j>)
{
j--;
} ncount = j - i + ;
//
strncpy(buf, p + i, ncount);
buf[ncount] = '\0';
return ret;
} //这种写法不好
//不要轻易去改变指针输入特性中in内存块的内存
int trimSpaceStr2_notgood(char *p)
{
int ret = ; int ncount = ;
int i = , j;
j = strlen(p) - ; while (isspace(p[i]) && p[i] != '\0')
{
i++;
} while (isspace(p[j]) && j>)
{
j--;
} ncount = j - i + ; strncpy(p, p + i, ncount);
p[ncount] = '\0';
return ret;
} void main()
{
{
char *p = " abcd ";
char buf[] = { };
trimSpaceStr2(p, buf);
printf("buf = %s\n", buf);
} {
char *p = " abcd ";
trimSpaceStr2_notgood(p);
printf("p = %s\n", p);
}
}

3.字符串反转模型

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //将某个字符串逆置
void main()
{
char p[] = "abcde";
char c;
char *p1 = p;
char *p2 = p + strlen(p) - ; while (p1 < p2)
{
c = *p1;
*p1 = *p2;
*p2 = c;
++p1;
--p2;
} printf("p:%s \n", p);
}

4.两个辅助指针变量挖字符串

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /*
有一个字符串符合以下特征(”abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";),要求写一个函数(接口),输出以下结果
1) 以逗号分割字符串,形成二维数组,并把结果传出;
2) 把二维数组行数运算结果也传出。
*/ int spitString(const char *buf1, char c, char buf[][], int *num)
{
char *p = NULL;
char *pTmp = NULL;
int ncount = ;
char myBuf[] = { }; //步骤1 初始化条件 pTmp,p都执行检索的开头
p = buf1;
pTmp = buf1;
while (*p != '\0')
{
//步骤2 strstr strchr,会让p后移 在p和pTmp之间有一个差值
p = strchr(p, c);
if (p == NULL) //没有找到则跳出来
{
break;
}
else
{
memset(myBuf, , sizeof(myBuf)); //挖字符串
strncpy(myBuf, pTmp, p - pTmp);
myBuf[p - pTmp] = '\0'; strcpy(buf[ncount], myBuf); ncount++;
//步骤3 让p和pTmp重新初始化,达到检索的条件
pTmp = p = p + ;
} } ;
*num = ncount;
return ;
} int spitString02(const char *buf1, char c, char buf[][], int *num)
{
int ret = ;
char *p = NULL;
char *pTmp = NULL;
int ncount = ;
if (buf1 == NULL || num == NULL)
{
return -;
}
//步骤1 初始化条件 pTmp,p都执行检索的开头
p = buf1;
pTmp = buf1;
while (*p != '\0')
{
//步骤2 strstr strchr,会让p后移 在p和pTmp之间有一个差值
p = strchr(p, c);
if (p == NULL) //没有找到则跳出来
{
break;
}
else
{ //挖字符串
strncpy(buf[ncount], pTmp, p - pTmp);
buf[ncount][p - pTmp] = '\0'; ncount++; //步骤3 让p和pTmp重新初始化,达到检索的条件
pTmp = p = p + ;
} } ;
*num = ncount;
return ret;
} void main()
{
int ret = , i = ;
const char *buf1 = "abcdef,acccd,";
char c = ',';
char buf[][];
int num = ;
ret = spitString02(buf1, c, buf, &num);
if (ret != )
{
printf("func spitString() err:%d\n", ret);
return ret;
} for (i = ; i<num; i++)
{
printf("%s\n", buf[i]);
} system("pause");
}