c复习day07

指针与字符串

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
#include <stdio.h>
#include <string.h>

int main(int argc,char *argv[])
{
//定义一个字符数组,数组的内容是helloworld\0
char a[] = "helloworld";
//定义一个指针来保存数组首元素的地址
char *p = a;
printf("%s\n",p);//打印一个字符串,要的是首字母的地址
printf("%s\n",p+2);
printf("%c\n",*(p+3));

printf("-------\n");
*p = 'm';
printf("%s\n",p);
p++;//指针p向后移动一个位置
*p = 'o';
printf("%s\n",p);
printf("--------\n");

printf("%d\n",sizeof(p));//在32位系统下4字节,在64位系统下为8字节
printf("%d\n",sizeof(a));//11

printf("--------\n");

printf("%d\n",strlen(p));//9
printf("%d\n",strlen(a));//10

return 0;
}

字符串常量

  • “hello” 字符串常量是不可以改变的,存在文字常量区
  • 在使用””时,代表取这个字符串首元素的地址
  • char *p = “hello”;//代表将字符串常量的地址赋值给指针p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>

int main()
{
char a[] = "helloworld";//定义一个字符数组
char *p = a;//定义一个指针用来保存数组首元素的地址
p = "abcdef";//字符串常量存在文字常量区,""在使用的时候取的是字符串首元素的地址
//文字常量区的内容是不可以改变的
printf("%s\n",p);

printf("%d\n",sizeof(p));//4,在64位下为8
printf("%d\n",sizeof("abcdef"));//7

printf("--------\n");

printf("%d\n", strlen(p));//6
printf("%d\n", strlen("abcdef"));//6

//*p = "m";错误
printf("%s\n",p);
return 0;
}

字符数组作为形参

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>
char * my_strcat(char * src,char *dst);
int main()
{
char str1[128] = "hello";
char str2[128] = "123456";
my_strcat(str1,str2);
printf("%s\n",my_strcat(str1,str2));
return 0;
}
char * my_strcat(char * src,char *dst)
{
int n = strlen(src);
int i = 0;
while (*(dst+i) != 0)
{
*(src+n+i) = *(dst+i);
i++;
}
*(src+n+i) = 0;
return src;
}

const修饰的指针变量

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int main()
{
char buf[] = "hello";
char str[] = "abcd";
const char *p = buf;//const修饰指针不能通过指针来修改所指向空间的内容
*p = 's';
char * const k = str;//指针变量初始化之后不能改变k指针变量的指向
//k = "world";err
//k = buf;err
return 0;
}

字符指针数组

  • 是一个数组,每一个元素是字符指针
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
#include <stdio.h>
int main()
{
// char *p1 = "hhhh";
// char *p2 = "nnnn";
// char *p3 = "mmmm";
char *num[3] = {"hafa","nina","mama"};
//定义一个指针用来保存num数组首元素的地址
char **p = num;
for (int i = 0; i < 3; i++)
{
// printf("%s\n",*(p+i));
printf("%s\n",p[i]);
}
printf("------\n");
printf("%c\n",*(*(p+1)+3));
printf("------\n");
for (int i = 0; i < 3; i++)
{
printf("%s\n",num[i]);
}
printf("------\n");
printf("%c\n",*num[0]);
printf("%c\n",*(num[1]+1));
printf("%c\n",*(num[2]+2));
return 0;
}

字符指针数组作为main函数的形参

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
int main(int argc,char *argv[])
{
printf("%d\n",argc);
for (int i = 0; i < argc; i++)
{
printf("%s\n",argv[i]);
}

return 0;
}

字符串处理函数(string.h)

strcpy

  • 此函数作为字符串拷贝函数
  • 将str2的字符拷贝至str1数组中,注意str2在遇到\0的时候会结束将\0拷贝至str1
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <string.h>
int main()
{
char str1[128] = "";
char str2[128] = "hello geek";
strcpy(str1,str2);
printf("%s\n",str1);

return 0;

}

strncpy

  • 与strcpy功能类似增加指定字符个数
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <string.h>
int main()
{
char str1[128] = "";
char str2[128] = "hello geek";
strncpy(str1,str2,5);
printf("%s\n",str1);

return 0;

}

strcat

  • 字符串的链接
  • 将字符str2字符数组中的字符连接到str1后面,遇到\0结束
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>
int main()
{
char str1[128] = "hello";
char str2[128] = "geek";
strcat(str1,str2);
printf("%s\n",str1);
printf("%s\n",str2);

return 0;

}

strncat

  • 参考strncpy功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>
int main()
{
char str1[128] = "hello";
char str2[128] = "geekboys";
// strcat(str1,str2);
strncat(str1,str2,7);
printf("%s\n",str1);
printf("%s\n",str2);

return 0;

}

strcmp

  • 字符串比较函数
  • 比较的是字符的ascii值
  • 如果str1 > str2返回值为1
  • 如果str1 < str2返回值为-1
  • 如果str1 与 str2相等返回值为0
  • 同理,使用strncmp指定了比较的个数
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <string.h>
int main()
{
char str1[128] = "geek";
char str2[128] = "geekk";

printf("%d\n", strcmp(str1, str2));
printf("%d\n", strncmp(str1, str2,3));
return 0;
}

sprintf组包函数

  • int len = sprintf(buf,”格式”,”数据”);//将数据安装格式组包,存放在数组buf中
  • sprintf函数的返回值是组完包的有效长度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>
int main()
{
char buf[1024];
int year = 2021;
int month = 4;
int day = 6;
int len = sprintf(buf,"year = %d,month = %d,day = %d",year,month,day);
printf("[%s]\n",buf);
printf("%d\n",len);

return 0;
}

sscnaf解包函数

  • sscanf(buf,”格式”,数据);//将buf的内容格式化输出到数据
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <string.h>
int main()
{
char buf[1024] = "2021-4-24";
int year = 0;
int month = 0;
int day = 0;
sscanf(buf,"%d-%d-%d",&year,&month,&day);
printf("%d %d %d\n",year,month,day);
return 0;
}

strchar

  • srtrchr(buf,ch)//在buf字符数组中查找字符ch的位置,如果成功返回此字符的地址
  • 如果没有找到返回NULL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <string.h>
char *my_strchr(char *p,char ch)
{
int i = 0;
while (p[i]!=0)
{
if(p[i] == ch)
return &p[i];
i++;
}
if(p[i] == 0)
return NULL;

}

int main()
{
char srt[] = "fdafasdfasdf";
char *P = strchr(str,'a');
//char *p = my_strchr(str,'a');
printf("%s",p);
return 0;
}

strstr

  • strstr(str1,str2)//在str1字符数组中查找str2字符串出现的位置,并且返回这个位置的地址
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
#include <stdio.h>
#include <string.h>
char *my_strstr(char *str1,char *str2);

int main()
{
char str1[] = "fafasdfabcfabc";
char str2[] = "abc";
//在str1中寻找str2字符出现的位置
//先找a字符,如果找到a字符之后再进行比较
}
char *my_strstr(char *str1,char *str2)
{
int i = 0;
while (str[i] != 0)
{
if (str1[i] == str2[0])
{
if( 0 == strncmp(str1+i,str2,strlen(str2)))
return str1+i;
}
i++;
}
if (str[i] == 0)
return NULL
}

strtok

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
#include <stdio.h>
#include <string.h>

int main()
{
char str[] = "nihao#woaini#xiangnila";
char *p[10] = {NULL};
// char *p1 = strtok(str,"#");
// printf("%s\n",p1);
// char *p2 = strtok(NULL,"#");
// printf("%s\n",p2);
// char *p3 = strtok(NULL, "#");
// printf("%s\n", p3);
int i = 0;
do
{
if (i == 0)
{
p[i] = strtok(str,"#");
} else {
p[i] = strtok(NULL,"#");
}


} while (p[i++] != NULL);
i = 0;
while (p[i] != NULL)
{
printf("%s\n",p[i]);
i++;
}
return 0;
}

atoi && atof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>

int main()
{
char str1[] = "13414";
char str2[] = "13.34";

//将字符串转换为整数
int a = atoi(str1);
double b = atof(str2);
printf("%d\n", a);
printf("%lf", b);
return 0;

}

字符串的反转

  • 事实上与前面的数组操作的相同
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>

int main()
{
char a[] = "hello";
char *start = a;
char *end = &a[strlen(a)-1];

while (start < end)
{
char b = *start;
*start = *end;
*end = b;
end--;
start++;

}

printf("%s\n",a);
return 0;

}

两头堵模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <string.h>
int main()
{
char buf[] = " hello world ";//hello world
char num[128] = "";
if (buf[0] == 0)
return 0;
char * start = buf;
char *end = &buf[strlen(buf) - 1];//end指向最后一个字符
//找到第一个不是空格的位置
while (*start == ' ' && *start != 0)
{
start++;
}
while (*end == ' ' && end != start)
{
end--;
}
printf("%d\n",end-start+1);
strncpy(num,start,end-start+1);
printf("num=%s\n",num);
return 0;
}

字符串中寻找字符串,统计次数

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
#include <string.h>
#include <stdio.h>
int strstr_num(char *src,char *dst);
int main()
{
char src[] = "afafasfsabcfdafabcfdafdsabc";
char dst[] = "abc";
int n = strstr_num(src,dst);
printf("%d\n",n);

return 0;
}

int strstr_num(char *src,char *dst)
{
int n = 0;
char *p = src;
do
{
p = strstr(p,dst);
//如果查到返回的地址不等于NULL,代表找到啦
if (p != NULL)
{
n++;
p = p + strlen(dst);
}

} while (p != NULL);
return n;

}
你的支持是我最大的动力!
0%