#include <stdio.h>
#include <string.h>
void primaryPart(void);
void myStrcpy(char *destination, const char *resource);
void myAStrcpy(char *destination, const char *resource);
void myStrncpy(char *destination, const char *resource, int length);
int characterCount(const char *);
void primaryCount(void);
int myStrcmp(const char *, const char *);
void Myitoa(int, char *);
int main(){
//{
// //primaryPart();
// char buf1[10];
// char *p = "nihaoa";
// char buf2[] = "hellosssssssss";
// //myStrncpy(buf1, buf2,6);
// //printf("%s\n", buf1);
// char chinese2[] = "我s爱s你";
// //printf("%s 的字符个数有:%d", chinese2, characterCount(chinese2));
//}
//{
// char s1[] = "bbc";
// char s3[] = "abc";
// char s2[] = "ab";
// char s[] = "";
// //printf("%d", myStrcmp("", ""));
// //printf("%d", 'b'-'c');
// *s == 0;
// //printf("%d", *s==0);//这里要加载符号
// printf("%d", myStrcmp("", s1));
//}
char s[4];
Myitoa(123, s);
printf("%s\n", s);
getchar();
return 0;
}
/**
废弃掉,重新写一个。
*/
void Myitoa(int number, char *myint){
char numCharacter[10] = { "123456789" };
int time = 0;
while (number / 10 != 0){
*(myint + time++) = numCharacter[number % 10];
number = number / 10;
}
}
/**
字符串比较.
库里面也没有比较 NULL的情况
测试用例不是很全,大概就是这个样子了。再有问题在修改吧。这种问题一般都会拖到无穷远。。。
*/
int myStrcmp(const char *str1, const char *str2){
/*int result = *str1++ - *str2++;
while (result == 0){
result = *str1++ - *str2++;
}*/
if (*str1 == 0 && *str2 == 0){
return 0;
}
int result = 0;
while ((result=*str1++ - *str2++ )== 0){
}
return result;
}
/**
前情提要
*/
void primaryCount(void){
char chinese[] = "我爱你";
char chinese2[] = "我s爱s你";
printf("%s\nsizeof(chinese)=%d\nstrlen(chinese)=%d\n", chinese, sizeof(chinese), strlen(chinese));//前面是7 后面是6.
printf("%s\nsizeof(chinese2)=%d\nstrlen(chinese2)=%d\n", chinese2, sizeof(chinese2), strlen(chinese2));//前面是9 后面是8.
//这样就不太好了,我们需要的是计数,一个字符算一个数儿。汉字是两个字节,英文字是1个字节。所以记录的结果是这样的。
}
/**
汉字是两个字节,英文字是1个字节。
希望一个汉字 或者 一个英文计数都只加1
这个 需要配一个图
这个代码绝对牛逼!!!全文亮点了,至少因为 我能看懂,并且也写得出来
*/
int characterCount(const char *p){
int result = 0;
while (*p != 0){
result++;
if (*p++ < 0){
p++;
}
}
return result;
}
/**
这个 不想改,这个 又好看懂并且执行也不慢。就这样了!!!不改了!!!一定要用最简单的方式 实现最复杂的功能!!!
避免溢出
*/
void myStrncpy(char *destination, const char *resource, int length){
for (int i = 0; i < length; i++){
*destination++ = *resource++;
}
*destination = 0;
}
/**
比strcpy高级一些的写法
*/
void myAStrcpy(char *destination, const char *resource){
while (*destination++ = *resource++);
}
/**
稍微 比较容易理解的拷贝方式
*/
void myStrcpy(char *destination, const char *resource){//会有溢出错误
while (*resource != '\0'){//这里 0跟 \0的效力是相同的
*destination++ = *resource++;
}
*destination = 0;
}
void primaryPart(void){
char s[] = "hello world";
printf("sizeof(s)=%d\nstrlen(s)=%d\n", sizeof(s), strlen(s));//两者在数值上相差1,sizeof因为 要保证结尾所以多一个字符的空间
//sizeof本质上是这段内容在内存中所占据的空间大小,而,strlen是表示这段内容中,纯字符的部分的大小。
char *p = &s;
//p[3] = 'a';
*(p + 3) = 'a';
printf("%s\n", s);
char *p1 = "hello world";
*(p1 + 3) = 'a';//虽然编译可以过,但是这里会出现问题。
printf("%s\n", p1);
}