1、使用指针实现 strcpy、strcat 函数的功能
#include <stdio.h>
int main(int argc, const char *argv[])
{
char s1[32] = "aabbcc";
char s2[32] = "112233";
char *p1 = &s1;
char *p2 = &s2;
while(*p1)
{
p1++;
}
while(*p2)
{
*p1 = *p2;
p1++;
p2++;
}
*p1 = *p2;//'\0'
p1 = s1;//重新让指针指向字符串首地址
p2 = s2;
printf("%s\n",p1);
printf("%s\n",p2);
return 0;
}
#include <stdio.h>
int main(int argc, const char *argv[])
{
char s1[32] = "helloqwer";
char s2[32] = "world";
char *p1 = s1;
char *p2 = s2;
while(*p2)
{
*p1++=*p2++;
}
*p1 = *p2;
p1 = s1;//重新让指针指向字符串首地址
p2 = s2;
printf("%s\n",p1);
printf("%s\n",p2);
return 0;
}
2、设计一个程序,判断操作系统是大端存储还是小端存储
#include <stdio.h>
int main(int argc, const char *argv[])
{
int a = 0x12345678;
char *p = &a;
if(*p=0x78)
{
printf("小\n");
}
else
{
printf("大\n");
}
return 0;
}
3、指针实现冒泡排序
#include <stdio.h>
int main(int argc, const char *argv[])
{
int arr[5]={8,9,4,2,5};
int *p = arr;
int i = 0;
int j = 0;
int temp = 0;
for(i=1;i<5;i++)
{
for(j=0;j<5-i;j++)
{
if(p[j]>p[j+1])
{
temp = p[j+1];
p[j+1] = p[j];
p[j] = temp;
}
}
}
for(i=0;i<5;i++)
{
printf("%d\n",p[i]);
}
return 0;
}
4、用指针的方式进行字符串倒叙。"abcdef" ==> "fedcba"
#include <stdio.h>
int main(int argc, const char *argv[])
{
char s1[]="abcdef";
char *p = s1;
int count = 0;
int temp = 0;
while(*p)
{
count++;
p++;
}
printf("%d\n",count);
for(int k=0;k<count/2;k++)
{
temp = s1[k];
s1[k]=s1[count-k-1];
s1[count-k-1]=temp;
}
puts(s1);
return 0;
}
5、终端获取字符串删除空格,用指针方式实现
#include <stdio.h>
int main(int argc, const char *argv[])
{
char s1[30]="";
gets(s1);
char *p=s1;
int i=0,j=0;
while (p[i]!='\0')
{
if (p[i]!=' ')
{
p[j] = p[i];
j++;
}
i++;
}
p[j] = '\0';
puts(s1);
return 0;
}