1.将字符串反转输出。
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 void string_reverse(const char *str,char *ptr)
6 {
7 int len = strlen(str),i;
8 str=str+(len-1);
9 for(i=0;i<len;i++)
10 {
11 *ptr=*str;
12 str--;
13 ptr++;
14 }
15 }
16
17 int main()
18 {
19 char *str = "helloworld!\n";
20 char *ptr = (char *)malloc(sizeof(char)*32);
21
22 string_reverse(str,ptr);
23 printf("%s\n",ptr);
24 return 0;
25 }
~
~
2.判断一个字符串是否为另一个字符串的子串。
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 int main()
6 {
7 char *str,*ptr;
8 int len_str,len_ptr,i;
9 str=(char *)malloc(sizeof(char)*32);
10 ptr=(char *)malloc(sizeof(char)*32);
11
12 printf("Please input two string:\n");
13 scanf("%s%s",str,ptr);
14
15 len_str = strlen(str);
16 len_ptr = strlen(ptr);
17
18 if(len_ptr>len_str)
19 {
20 printf("Input Error");
21 }
22 for(i=0;i<len_str-len_ptr+1;i++)
23 {
24 if(strncmp(str+i,ptr,len_ptr) == 0)
25 {
26 printf("%s是%s的子串",ptr,str);
27 break;
28 }
29 if(i == len_str-len_ptr)
30 {
31 printf("%s不是%s的子串",ptr,str);
32 }
33 }
34 printf("\n");
35 return 0;
36 }
~
~
#include"stdio.h" 在当前目录和TC指定的目录中找该文件。
#include<stdio.h> 在由TC指定的文件目录中找该文件。
#define OUT printf("helloworld") //无参宏函数
#define P(s) printf("%s\n",s) //有参的宏函数
自定义函数和宏函数的区别:宏函数优点:节省空间(形参);缺点:浪费时间(编译) ;没有语法检查,不安全。
普通函数 优点:有语法检查,缺点:浪费时间
扫描二维码关注公众号,回复:
2484730 查看本文章
1.指针:指针的长度都为4个字节
2.指针的使用步骤:1定义指针变量;
2给指针变量赋地址;
3正常使用指针进行运算。