1.求非空格的字符串长度
- 要求:某一字符串两边为空格,求非空格字符串长度
- 如:char *p = ” abdcefg “;
(1)代码
方法一
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void main()
{
//求非空格的字符串长度
char *p = " abdcefg ";
int i, j = 0;
i = 0;
j = strlen(p) - 1;
int ncount = 0;
while (isspace(p[i]) && p[i] != '\0')
{
i++;
}
while (isspace(p[j]) && p[j] != '\0')
{
j--;
}
ncount = j - i + 1;
printf("ncount:%d", ncount);
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
方法二
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//求非空格的字符串长度
void getCount(char *str, int *pCount)
{
char *p = str;
int ncount = 0;
int i, j = 0;
if (str == NULL || pCount == NULL)
{
return -1;
}
i = 0;
j = strlen(p) - 1;
while (isspace(p[i]) && p[i] != '\0')
{
i++;
}
while (isspace(p[j]) && p[j] != '\0')
{
j--;
}
ncount = j - i + 1;
*pCount = ncount;
}
void main()
{
char *p = " abcdefg ";
int count = 0;
getCount(p, &count);
printf("count:%d\n", count);
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
(2)运行结果
(3)图解
2.去掉字符串前后空格
(1)代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//去掉字符串前后空格
void trimSpace(char *str, char *newstr)
{
char *p = str;
int ncount = 0;
int i, j = 0;
if (str == NULL || newstr == NULL)
{
printf("func trimSpace() \n");
return -1;
}
i = 0;
j = strlen(p) - 1;
while (isspace(p[i]) && p[i] != '\0')
{
i++;
}
while (isspace(p[j]) && p[j] != '\0')
{
j--;
}
ncount = j - i + 1;
strncpy(newstr, str + i, ncount);
newstr[ncount] = '\0';
}
void main()
{
char *p = " abcdefg ";
char buf[1024] = { 0 };
trimSpace(p, buf);
printf("p:%s\n", p);
printf("buf:%s\n", buf);
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
(2)运行结果
3.字符串反转
(1)方法一
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void main()
{
char buf[] = "abcdefg";
int length = strlen(buf);
char *p1 = buf;
char *p2 = buf + length - 1;
while (p1 < p2)
{
char c = *p1;
*p1 = *p2;
*p2 = c;
++p1;
--p2;
}
printf("buf:%s\n",buf);
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
(2)方法二:通过递归的方式,逆向打印
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//通过递归的方式,逆向打印
void inverse(char *p)
{
if (p == NULL)//递归结束的异常条件
{
return;
}
if (*p == '\0')//递归结果条件
{
return;
}
inverse02(p + 1);//注意此时没有执行打印,而是执行了调用函数
//让字符串的每一个地址入栈
printf("%c", *p);
}
void main()
{
char buf[] = "abcdefg";
inverse(buf);
system("pause");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
(3)方法三:递归和全局变量(把逆序的结果存入全局变量)
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char g_buf[1024];
void inverse(char *p)
{
if (p == NULL)//递归结束的异常条件
{
return;
}
if (*p == '\0')//递归结果条件
{
return;
}
inverse(p + 1);//注意此时没用执行打印,而是执行了调用函数
//让字符串的每一个地址入栈
strncat(g_buf, p, 1);
}
void main()
{
char buf[] = "abcde";
memset(g_buf, 0, sizeof(g_buf));
inverse(buf);
printf("g_buf:%s\n",g_buf);
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
(4)方法四:递归和非全局变量(递归指针做函数参数)
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void inverse(char *p, char *bufresult)
{
if (p == NULL)//递归结束的异常条件
{
return;
}
if (*p == '\0')//递归结果条件
{
return;
}
inverse(p + 1, bufresult);//注意此时没用执行打印,而是执行了调用函数
//让字符串的每一个地址入栈
strncat(bufresult, p, 1);
}
void main()
{
char buf[] = "abcde";
{
char mybuf[1024] = { 0 };
inverse(buf,mybuf);
printf("递归和局部变量在一起mybuf:%s\n", mybuf);
}
system("pause");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
4.键值对字符串
- 要求1:求自己定义一个接口,实现根据key获取value
- 要求2:编写测试用例
- 要求3:键值对中间可能有n多个空格,求去除空格
如:
"key1= value1";
"key2= value2 ";
"key3=value3 ";
"key4= value4";...
- 1
- 2
- 3
- 4
- 5
代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/*=====================================
键值对字符串在开发中经常使用
要求1:求自己定义一个接口,实现根据key获取value
要求2:编写测试用例
要求3: 键值对中间可能有n多个空格,求去除空格
===========================================*/
//去掉字符串前后空格
int trimSpace1(char *str, char *newstr)
{
char *p = str;
int ncount = 0;
int i, j = 0;
if (str == NULL || newstr == NULL)
{
printf("func trimSpace() \n");
return -1;
}
i = 0;
j = strlen(p) - 1;
while (isspace(p[i]) && p[i] != '\0')
{
i++;
}
while (isspace(p[j]) && p[j] != '\0')
{
j--;
}
ncount = j - i + 1;
strncpy(newstr, str + i, ncount);
newstr[ncount] = '\0';
return 0;
}
//根据key获取value
int getValueByKey(char *keyvaluebuf,char *keybuf,char *valuebuf)
{
char *p = NULL;
int ret = 0;
if (keyvaluebuf == NULL || keybuf == NULL || valuebuf == NULL)
{
return -1;
}
//1.查找key是不是在母串中
p = keyvaluebuf;
p = strstr(p, keybuf);
if (p == NULL)
{
return -1;
}
//让辅助指针变量 重新达到下一次检索的条件
p = p + strlen(keybuf);
//2.看有没有=号
p = strstr(p, "=");
if (p == NULL)
{
return -1;
}
//让辅助指针变量 重新达到下一次检索的条件
p = p + strlen("=");
//3.在等号后面 去除空格
ret = trimSpace1(p, valuebuf);
if (ret != 0)
{
printf("func trimSpace1() err:%d \n",ret);
return ret;
}
return 0;
}
int main()
{
int ret = 0;
int buf[1024];
char *keyandvalue = "key2= value2 ";
char *key = "key2";
ret = getValueByKey(keyandvalue,key,buf);
if (ret != 0)
{
printf("func getKeyByValue() err:%d \n",ret);
return ret;
}
printf("buf:%s\n",buf);
system("pause");
return ret;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
运行结果
5.const小专题
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void getmem1(const char *p)
{
p = 1;
p = 3;
//p[1]='a'; 报错
return;
}
void getmem2(char *const p)
{
//p = 1;报错
//p = 3;报错
p[1]='a';
return;
}
void getmem3(const char *const p)
{
//p = 1;报错
//p = 3;报错
//p[1] = 'a';
printf("%c", p[1]);
return;
}
void main01()
{
char *p1 = NULL;
const char *p2 = NULL;
p2 = 1;
printf("hello...\n");
system("pause");
return;
}
void main()
{
const int a = 10;
//a = 11;
{
int *p = &a;
*p = 100;
printf("a:%d \n", a);
//结论:C语言中的const修饰的变量是假的,C语言中的const是一个冒牌货
}
printf("hello...\n");
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
结论
- 指针变量和它所指向的内存空间变量,是两个不同的概念
- 看const是放在*左边还是右边,看const是修饰指针变量,还是修饰所指向的内存空间变量
6.二级指针做输出的特性
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//指针做输出:被调用函数分配内存
int getMem(char **myp1, int *mylen1, char **myp2, int *mylen2)
{
char *tmp1 = NULL;
char *tmp2 = NULL;
tmp1 = (char *)malloc(100);
if (tmp1 == NULL)
{
return -1;
}
strcpy(tmp1, "abcdefg");
*mylen1 = strlen(tmp1);
*myp1 = tmp1; //间接修改实参p1的值
tmp2 = (char *)malloc(100);
if (tmp2 == NULL)
{
return -2;
}
strcpy(tmp2, "11122233333");
*mylen2 = strlen(tmp2);
*myp2 = tmp2; //间接修改实参p1的值
return 0;
}
int getMem_Free(char **myp1)
{
/*
if (myp1 == NULL)
{
return ;
}
free(*myp1); //释放完指针变量 所致的内存空间
*myp1 = NULL; //把实参修改成nULL
*/
char *tmp = NULL;
if (myp1 == NULL)
{
return -1;
}
tmp = *myp1;
free(tmp); //释放完指针变量 所致的内存空间
*myp1 = NULL; //把实参修改成nULL
return 0;
}
void main()
{
char *p1 = NULL;
int len1 = 0;
char *p2 = NULL;
int len2 = 0;
int ret = 0;
ret = getMem(&p1, &len1, &p2, &len2);
printf("p1: %s \n", p1);
printf("p2: %s \n", p2);
getMem_Free(&p1);
getMem_Free(&p2);
printf("p1: %s \n", p1);
printf("p2: %s \n", p2);
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
7.二级指针做输入的特性
(1)第一种模型(指针数组排序、打印)
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//打印
void printMyArray(char **myArray, int num)
{
int i = 0;
for (i = 0; i < num; i++)
{
//printf("%s \n", myArray[i]);
printf("%s \n", *(myArray + i));
}
}
//排序
void sortMyArray(char **myArray, int num)
{
int i = 0, j = 0;
char *tmp = NULL;
//排序
for (i = 0; i < num; i++)
{
for (j = i; j<num; j++)
{
if (strcmp(myArray[i], myArray[j]) > 0)
{
tmp = myArray[i]; //注意 交换的是数组元素 交换的是指针的值 //改变指针的指向
myArray[i] = myArray[j];
myArray[j] = tmp;
}
}
}
}
void main()
{
int i = 0, j = 0;
int num = 0;
char *tmp = NULL;
//数组 数组中的每一个元素是指针 指针数组
char *myArray[] = { "aaaaaa", "ccccc", "bbbbbb", "111111" };
//打印
num = sizeof(myArray) / sizeof(myArray[0]);
printf("排序之前\n");
printMyArray(myArray, num);
sortMyArray(myArray, num);
//排序
printf("排序之后\n");
printMyArray(myArray, num);
printf("hello...\n");
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
(2)第二种模型(二维数组排序、打印)
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void printMyArray(char myArray[10][30], int num)
{
int i = 0;
for (i = 0; i < num; i++)
{
//printf("%s \n", myArray[i]);
printf("%s \n", *(myArray + i)); //
}
}
//交换的是内存块
void sortMyArray(char myArray[10][30], int num)
{
int i, j = 0;
char tmpBuf[30];
for (i = 0; i < num; i++)
{
for (j = i + 1; j<num; j++)
{
if (strcmp(myArray[i], myArray[j]) > 0)
{
strcpy(tmpBuf, myArray[i]); //交换的是内存块
strcpy(myArray[i], myArray[j]);
strcpy(myArray[j], tmpBuf);
}
}
}
}
void main()
{
int i = 0, j = 0;
int num = 4;
char myBuf[30];
char tmpBuf[30];
char myArray[10][30] = { "aaaaaa", "ccccc", "bbbbbbb", "1111111111111" };
//打印
printf("排序之前\n");
printMyArray(myArray, num);
sortMyArray(myArray, num);
//打印
printf("排序之后\n");
printMyArray(myArray, num);
printf("hello...\n");
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
(3)第三种模型(手工二维内存)
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void main()
{
int i = 0, j = 0;
char **p2 = NULL;
int num = 5;
char *tmp = NULL;
char tmpbuf[100];
p2 = (char **)malloc(sizeof(char *)* num);
for (i = 0; i < num; i++)
{
p2[i] = (char *)malloc(sizeof(char)* 100); //char buf[100];
sprintf(p2[i], "%d%d%d", i + 1, i + 1, i + 1);
}
//排序之前
printf("排序之前\n");
for (i = 0; i < num; i++)
{
printf("%s \n", p2[i]);
}
//排序 交换的是指针指向
/*
for (i=0; i<num; i++)
{
for (j=i+1; j<num; j++)
{
if (strcmp( p2[i] , p2[j]) < 0)
{
tmp = p2[i];
p2[i] = p2[j];
p2[j] = tmp;
}
}
}
*/
//排序 交换的是内存的值
for (i=0; i<num; i++)
{
for (j=i+1; j<num; j++)
{
if (strcmp( p2[i] , p2[j]) < 0)
{
strcpy(tmpbuf, p2[i]);
strcpy( p2[i], p2[j]);
strcpy( p2[j], tmpbuf);
}
}
}
//排序之后
printf("排序之后\n");
for (i = 0; i < num; i++)
{
printf("%s \n", p2[i]);
}
//释放内存
for (i = 0; i < num; i++)
{
if (p2[i] != NULL)
{
free(p2[i]);
p2[i] = NULL;
}
}
if (p2 != NULL)
{
free(p2);
}
printf("hello...\n");
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
代码优化(封装函数)
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char **getMem11(int num)
{
int i = 0;
char **p2 = NULL;
p2 = (char **)malloc(sizeof(char *)* num);
if (p2 == NULL)
{
return NULL;
}
for (i = 0; i < num; i++)
{
p2[i] = (char *)malloc(sizeof(char)* 100); //char buf[100];
sprintf(p2[i], "%d%d%d", i + 1, i + 1, i + 1);
}
return p2;
}
void printMyArray(char **myArray, int num)
{
int i = 0;
for (i = 0; i < num; i++)
{
//printf("%s \n", myArray[i]);
printf("%s \n", *(myArray + i));
}
}
void sortMyArray(char **myArray, int num)
{
int i = 0, j = 0;
char *tmp = NULL;
//排序
for (i = 0; i < num; i++)
{
for (j = i; j<num; j++)
{
if (strcmp(myArray[i], myArray[j]) <0)
{
tmp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = tmp;
}
}
}
}
void getMem11_Free(char **p2, int num)
{
int i = 0;
//释放内存
for (i = 0; i < num; i++)
{
if (p2[i] != NULL)
{
free(p2[i]);
p2[i] = NULL;
}
}
if (p2 != NULL)
{
free(p2);
}
}
void main()
{
int i = 0, j = 0;
char **p2 = NULL;
int num = 5;
char *tmp = NULL;
char tmpbuf[100];
p2 = getMem11(num);
//排序之前
printf("排序之前\n");
printMyArray(p2, num);
sortMyArray(p2, num);
//排序 交换的是内存
//排序之后
printf("排序之后\n");
printMyArray(p2, num);
//释放内存
getMem11_Free(p2, num); //p2是一个野指针
printf("hello...\n");
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
(4)三种模型的内存图
void main()
{
int i = 0;
//指针数组
char * p1[] = {"123", "456", "789"};
//二维数组
char p2[3][4] = {"123", "456", "789"};
//手工二维内存
char **p3 = (char **)malloc(3 * sizeof(char *)); //int array[3];
for (i=0; i<3; i++)
{
p3[i] = (char *)malloc(10*sizeof(char)); //char buf[10]
sprintf(p3[i], "%d%d%d", i, i, i);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
8.将字符串按逗号分开,并放入二维数组打印出来
- 如:char *p1 = “aaaa,bbbb,cccc,dddd,1111,2222,”;
方法一
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int spitString(const char *buf1, char c, char buf2[10][30], int *count)
{
char *p = NULL, *pTmp = NULL;
int tmpcount = 0;
//1 p和ptmp初始化
p = buf1;
pTmp = buf1;
do
{
//2 检索符合条件的位置 p后移 形成差值 挖字符串
p = strchr(p, c);
if (p != NULL)
{
if (p - pTmp > 0)
{
strncpy(buf2[tmpcount], pTmp, p - pTmp);
buf2[tmpcount][p - pTmp] = '\0'; //把第一行数据变成 C风格字符串
tmpcount++;
//3重新 让p和ptmp达到下一次检索的条件
pTmp = p = p + 1;
}
}
else
{
break;
}
} while (*p != '\0');
*count = tmpcount;
return 0;
}
void main()
{
int ret = 0, i = 0;
char *p1 = "aaaa,bbbb,cccc,dddd,1111,2222,";
char cTem = ',';
int nCount;
char myArray[10][30];
ret = spitString(p1, cTem, myArray, &nCount);
if (ret != 0)
{
printf("fucn spitString() err: %d \n", ret);
return ret;
}
for (i = 0; i < nCount; i++)
{
printf("%s \n", myArray[i]);
}
printf("hello...\n");
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
方法二
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//有一个字符串符合以下特征
"dasd,saddasd,gfgfgg,aaaa,fwfsadqw,sssss,";
//分清楚赋值指针变量 和 操作逻辑之间的关系
int spitString(const char *buf1, char c, char **myp /*in*/, int *count)
{
char *p=NULL, *pTmp = NULL;
int tmpcount = 0;
//1 p和ptmp初始化
p = buf1;
pTmp = buf1;
do
{
//2 检索符合条件的位置 p后移 形成差值 挖字符串
p = strchr(p, c);
if (p != NULL)
{
if (p-pTmp > 0)
{
strncpy(myp[tmpcount], pTmp, p-pTmp);
myp[tmpcount][p-pTmp] = '\0'; //把第一行数据变成 C风格字符串
tmpcount ++;
//3重新 让p和ptmp达到下一次检索的条件
pTmp = p = p + 1;
}
}
else
{
break;
}
} while (*p!='\0');
*count = tmpcount;
return 0;
}
void main()
{
int ret = 0, i = 0;
char *p1 = "dasd,saddasd,gfgfgg,aaaa,fwfsadqw,sssss,";
char cTem= ',';
int nCount;
char **p = NULL; //相当于char buf[10][30]
p = (char **)malloc(10 * sizeof(char *)); // char * array[10]
if (p == NULL)
{
return;
}
for (i=0; i<10; i++)
{
p[i] = (char *)malloc(30 * sizeof(char));
}
ret = spitString(p1, cTem, p, &nCount);
if (ret != 0)
{
printf("fucn spitString() err: %d \n", ret);
return ret;
}
for (i=0; i<nCount; i++ )
{
printf("%s \n", p[i]);
}
//释放内存
for (i=0; i<10; i++)
{
free(p[i]);
}
free(p);
printf("hello...\n");
system("pause");
return ;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
方法三
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char ** spitString(const char *buf1, char c, int *count)
{
char *p = NULL, *pTmp = NULL;
int tmpcount = 0;
char **myp = NULL;
//1 p和ptmp初始化
p = buf1;
pTmp = buf1;
//第一遍求出count
do
{
//2 检索符合条件的位置 p后移 形成差值 挖字符串
p = strchr(p, c);
if (p != NULL)
{
if (p - pTmp > 0)
{
tmpcount++;
//3重新 让p和ptmp达到下一次检索的条件
pTmp = p = p + 1;
}
}
else
{
break;
}
} while (*p != '\0');
*count = tmpcount;
//根据多少行 精确的分配内存
myp = (char **)malloc(tmpcount * sizeof(char *));
if (myp == NULL)
{
return NULL;
}
tmpcount = 0;
//1 p和ptmp初始化
p = buf1;
pTmp = buf1;
do
{
//2 检索符合条件的位置 p后移 形成差值 挖字符串
p = strchr(p, c);
if (p != NULL)
{
if (p - pTmp > 0)
{
int len = p - pTmp + 1;
myp[tmpcount] = (char *)malloc(len * sizeof(char));
if (myp[tmpcount] == NULL)
{
return NULL;
}
strncpy(myp[tmpcount], pTmp, p - pTmp);
myp[tmpcount][p - pTmp] = '\0';
tmpcount++;
//3重新 让p和ptmp达到下一次检索的条件
pTmp = p = p + 1;
}
}
else
{
break;
}
} while (*p != '\0');
return myp;
}
void main()
{
int ret = 0, i = 0;
char *p1 = "abcdef,acccd,sdasdd,dsfdffds,eqwwqew,dasdsd,";
char cTem = ',';
int nCount;
char **p = NULL; //char buf[10][30]
p = spitString(p1, cTem, &nCount);
if (p == NULL)
{
printf("fucn spitString() err: %d \n", ret);
return ret;
}
for (i = 0; i < nCount; i++)
{
printf("%s \n", p[i]);
}
//释放内存
for (i = 0; i < nCount; i++)
{
free(p[i]);
}
free(p);
printf("hello...\n");
system("pause");
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
优化代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void FreeMem(char ***p, int count)
{
int i = 0;
char **myp = NULL;
if (p == NULL)
{
return;
}
myp = *p;
if (myp == NULL)
{
return;
}
for (i = 0; i < count; i++)
{
if (myp[i] != NULL)
{
free(myp[i]);
}
}
if (myp != NULL)
{
free(myp);
}
*p = NULL; //把实参二级指针 ,修改成NULL
}
int spitString(const char *buf1, char c, char ***myp3, int *count)
{
int ret = 0;
char *p = NULL, *pTmp = NULL;
int tmpcount = 0;
char **myp = NULL;
//1 p和ptmp初始化
p = buf1;
pTmp = buf1;
//第一遍求出count
do
{
//2 检索符合条件的位置 p后移 形成差值 挖字符串
p = strchr(p, c);
if (p != NULL)
{
if (p - pTmp > 0)
{
tmpcount++;
//3重新 让p和ptmp达到下一次检索的条件
pTmp = p = p + 1;
}
}
else
{
break;
}
} while (*p != '\0');
*count = tmpcount;
//根据多少行 精确的分配内存
myp = (char **)malloc(tmpcount * sizeof(char *));
if (myp == NULL)
{
ret = -1;
printf("func spitString4() err:%d (tmpcount * sizeof(char *) )", ret);
goto END;
//return -1;
}
memset(myp, 0, tmpcount * sizeof(char *));
/*============================================================*/
tmpcount = 0;
//1 p和ptmp初始化
p = buf1;
pTmp = buf1;
do
{
//2 检索符合条件的位置 p后移 形成差值 挖字符串
p = strchr(p, c);
if (p != NULL)
{
if (p - pTmp > 0)
{
int len = p - pTmp + 1;
myp[tmpcount] = (char *)malloc(len * sizeof(char));
if (myp[tmpcount] == NULL)
{
ret = -2;
printf("func spitString4() err:%d malloc(len * sizeof(char) )", ret);
goto END;
}
strncpy(myp[tmpcount], pTmp, p - pTmp);
myp[tmpcount][p - pTmp] = '\0'; //把第一行数据变成 C风格字符串
tmpcount++;
//3重新 让p和ptmp达到下一次检索的条件
pTmp = p = p + 1;
}
}
else
{
break;
}
} while (*p != '\0');
END:
if (ret != 0) //失败
{
FreeMem(&myp, *count);
}
else
{
*myp3 = myp; //成功
}
return ret;
}
void main()
{
int ret = 0, i = 0;
char *p1 = "abcdef,acccd,";
char cTem = ',';
int nCount;
char **p = NULL; //char buf[10][30]
ret = spitString(p1, cTem, &p, &nCount);
if (ret != 0)
{
printf("fucn spitString() err: %d \n", ret);
return ret;
}
for (i = 0; i < nCount; i++)
{
printf("%s \n", p[i]);
}
//释放内存
for (i = 0; i < nCount; i++)
{
free(p[i]);
}
free(p);
printf("hello...\n");
system("pause");
return;
}