字符串复制和比较函数的实现
2015-10-27 11:07
363 查看
#include<stdio.h> #include<string.h> #define N 50 //字符串复制函数1,数组法(用虚数组做形参,以下标法访问元素) void strcpy1(char s1[], char s2[]) { int i = 0; while(s1[i] = s2[i]) i++; } //字符串复制函数2,指针法(用字符指针做形参,以指针法访问元素) void strcpy2(char *s1, char *s2) { while(*s1++ = *s2++); } //字符串比较函数,数组法 int strcmp1(char s1[], char s2[]) { int i= 0; while(s1[i] == s2[i]) { if(s1[i] == 0) return 0; i++; } return (s1[i] - s2[i]); } //字符串比较函数,指针法 int strcmp2(char *s1, char *s2) { while(*s1 == *s2) { if(*s1 == 0) return 0; s1++; s2++; } return (*s1 - *s2); } int main(void) { char str1[] = "welcome to Hello World!", str2 , str3 ; char st1[] = "welcome to China!", st2[] = "welcome to hello world!"; strcpy1(str2, str1); puts(str2); printf("************************\n"); strcpy2(str3, str1); puts(str3); printf("************************\n"); strcpy1(str2 + 11, "Beijing"); puts(str2); printf("************************\n"); printf("str1: "); puts(str1); printf("st1: "); puts(st1); printf("st2:"); puts(st2); printf("\nstr1与st1的比较结果:%d\n",strcmp1(str1, st1)); printf("\nstr1与st2的比较结果:%d\n",strcmp2(str1, st2)); printf("\nstr1与str2的比较结果:%d\n",strcmp2(str1, str2)); }
相关文章推荐
- VS2013无法启动 IIS Express Web的解决方法(全程图解)
- 设置textview颜色字体大小
- C#中Dictionary,Hashtable,List的比较及分析
- (原创)c#学习笔记06--函数05--函数的重载
- Python selenium chrome 环境配置
- Redis学习笔记(六)类型之有序集合
- 基于Jquery+div+css实现弹出登录窗口(代码超简单)
- codeforces 545 E. Paths and Trees
- 如何在App中实现IM功能之五快速实现图片接收和发送——箭扣科技Arrownock
- 手机网页开发学习(不定时更新)
- gsoap搭建WebService服务和客户端
- Web 2.0应用客户端性能问题十大根源《转载》
- H5多文本换行
- Image Sampling and Nyquist Theorem (summary)
- Linux-V4L2研读
- Flume采用zookeeper管理配置
- asp.net操作Word实现批量替换
- android.support
- C++中的extern "C" {}
- 腾讯设计导航