#include <stdio.h>
#include <stdlib.h>
#include <string.h> /*
_Check_return_ _Ret_maybenull_
inline char* __CRTDECL strstr(_In_z_ char* const _String, _In_z_ char const* const _SubString)
{
return const_cast<char*>(strstr(static_cast<char const*>(_String), _SubString));
}
*/ /*
对于字符串查找问题,可使用双重 for 循环解决,
效率更高的则为 KMP 算法。双重 for 循环的使用较有讲究,
因为这里需要考虑目标字符串比源字符串短的可能。
对目标字符串的循环肯定是必要的,所以可以优化的地方就在于如何访问源字符串了。
简单直观的解法是利用源字符串的长度作为 for 循环的截止索引,
这种方法需要处理源字符串中剩余长度不足以匹配目标字符串的情况,
而更为高效的方案则为仅遍历源字符串中有可能和目标字符串匹配的部分索引。 */
char *mystrstr1(char* const _String, char const* const _Substring)// 下标法
{
if (_String == NULL || _Substring == NULL)
{
return NULL;
}
char *pres = NULL;
int strLength = strlen(_String); // 母串的长度
int subLength = strlen(_Substring); // 子串的长度 for (int i = ; i < strLength - subLength + ; i++)// 获取要前进的尺度
{
int flag = ; // 假定相等
for (int j = ; j < subLength; j++)
{ if (_Substring[j] != _String[i+j])// 循环对比
{
flag = ;
break;
}
}
if (flag)
{
pres = _String + i; // 找到的地址
return pres;
}
} return pres; } char *mystrstr2(char * const _String, char * const _Substring)// 指针法
{
if (_String == NULL || _Substring == NULL)
{
return NULL;
}
char *pbak = _String; // 备份首地址
while (*pbak != '\0')
{
int flag = ; // 假定相等
char *pfind = pbak; // 从当前字符循环母串
char *psub = _Substring; // 从当前字符循环子串
while (*psub != '\0')
{
if (*pfind != '\0') // 母串提前结束
{
if (*pfind != *psub) // 判断字符串不等
{
flag = ;
break;
}
else
{
pfind++;
psub++; // 指针往前移动
}
}
else
{
flag = ;
break;
}
}
if (flag)
{
return pbak; // 保存当前地址
} pbak++; // 指针不断向前移动,遍历母串的每一个字符
} return NULL; } void main()
{
char str[] = "hello,NoThx";
char sub[] = "x"; //char *p = strstr(str, sub);
char *p = mystrstr1(str, sub);
//char *p = mystrstr2(str, sub);
if (p != NULL)
{ printf("找到 %p %c", p, *p);
}
else
{
printf("没找到");
} system("pause");
}
 #include <stdio.h>
#include <stdlib.h>
#include <string.h> /* size_t __cdecl strlen(_In_z_ char const* _Str); */ // 下标法
unsigned int mystrlen(const char * str)
{
int length = ;
/* for循环
for (int i = 0;;i++)
{
if (*(str + i) == '\0') //下标法 *(str + i) <==> str[i]
{
break;
}
length++;
} */
/* while循环
int i = 0;
while (1)
{ if (str[i] == '\0')
{
break;
}
else
{
length++;
i++;
}
} */ int i = ;
do
{
if (str[i] == '\0')
{
break;
}
else
{
length++;
i++;
}
} while (); return length;
} unsigned int mystrlenaddr(const char * str)// const说明只能读取,不能改变
{
int length = ;
for (const char *p = str; *p != '\0'; p++)
{
length++;
} return length;
} void main()
{
char *str = "afasfa"; //int lenth = mystrlen(str);
int lenth = mystrlenaddr(str); printf("%d\n", lenth); system("pause");
}
 #include <stdio.h>
#include <stdlib.h>
#include <string.h> /*
_ACRTIMP errno_t __cdecl strcpy_s(
_Out_writes_z_(_SizeInBytes) char* _Destination,
_In_ rsize_t _SizeInBytes,
_In_z_ char const* _Source
);
*/ char *mystrcpy(char *dest, const char *source)
{
if (dest == NULL || source == NULL)
{
return NULL;
}
for (int i = ; ; i++)// 下标法
{
dest[i] = source[i];
if (*(source + i) == '\0')
{
break;
} }
return dest;
} char *mystrcpyaddr(char *dest, const char *source)
{
if (dest == NULL || source == NULL)
{
return NULL;
}
char *phead = dest; while ((*dest++ = *source++))
{ }
return phead;
} void main()
{
char str[] = { };
char *p = "hello,how are you";
//strcpy_s(str, 100, p); // 字符串拷贝 printf("%s\n", mystrcpyaddr(str, p));
int x = , y = -; //printf("x %% y = %d\n", x%y); system("pause");
}

实现字符串检索strstr函数、字符串长度strlen函数、字符串拷贝strcpy函数的更多相关文章

  1. C 语言 字符串命令 strstr()的用法 实现将原字符串以分割串分割输出

    C 语言 字符串命令 strstr()的用法 实现将原字符串以分割串分割输出 strstr() 命令是在原字符串中查找指定的字符串第一次出现的地址,用这个特性可以实现字符的分割,判断是否包涵等功能: ...

  2. iOS 生成随机字符串 从指定字符串随机产生n个长度的新字符串

    随机字符串 - 生成指定长度的字符串 -(NSString *)randomStringWithLength:(NSInteger)len { NSString *letters = @"a ...

  3. PHP基础语法: echo,var_dump, 常用函数:随机数:拆分字符串:explode()、rand()、日期时间:time()、字符串转化为时间戳:strtotime()可变参数的函数:PHP里数组长度表示方法:count($attr[指数组]);字符串长度:strlen($a)

    PHP语言原理:先把代码显示在源代码中,再通过浏览器解析在网页上 a. 1.substr;  //用于输出字符串中,需要的某一部分 <?PHP $a="learn php"; ...

  4. 使用PHP的strstr()函数来统计一段字符串中元音字母的个数(区分大小写)

    <?php/**练习:统计一段字符串中所有元音字母的个数(区分大小写)*/$str='This is a test file.'; //原始字符串echo $str.'<br>'; ...

  5. 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset

    bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...

  6. PHP 语法字符串函数 strcmp、strlen 使用及实现

    说明 这里基于 php7.2.5 进行测试,php7 之后内部结构变化应该不是太大,但与 php5.X 有差别. 函数分类 用户自定义函数 say(); function say() { echo & ...

  7. 字符串s中从第i个位置起取长度为len的子串,函数返回子串链表

    /*已知字符串采用带结点的链式存储结构(详见linksrting.h文件),请编写函数linkstring substring(linkstring s,int i,int len),在字符串s中从第 ...

  8. 38 写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

    题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. public class _038PrintLength { public static void main(Stri ...

  9. 写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度

    import java.util.Scanner; /** * [程序38] * * 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. * * @author Jame ...

随机推荐

  1. openssl 编程

    背景: 生成私钥.公钥 --> 生成AES-key seed[32], iv[16] --> 公钥加密ASE-key, IV,传给Server --> Server用私钥解密,得到A ...

  2. vue-cli配置axios,并基于axios进行后台请求函数封装

    文章https://www.cnblogs.com/XHappyness/p/7677153.html已经对axios配置进行了说明,后台请求时可直接this.$axios直接进行.这里的缺点是后端请 ...

  3. Mybatis 点点滴滴

    相比 Hibernate ,Mybatis 实在是学习门槛低多了. 1 . 类属性和表字段的自动对应 当向数据库中插入一行数据时,<insert>标签中的占位符#{}中的占位符的值写 mo ...

  4. js & array to string

    js & array to string https://stackoverflow.com/questions/13272406/convert-string-with-commas-to- ...

  5. IE8 没有内容的盒子,如果有定位,浮现在其他盒子上 可能会有点击穿透没有作用的情况

    IE8 没有内容的盒子,如果有定位,浮现在其他盒子上 可能会有点击穿透没有作用的情况

  6. 【Python】Python发展历史

    起源 Python的作者,Guido von Rossum,荷兰人.1982年,Guido从阿姆斯特丹大学获得了数学和计算机硕士学位.然而,尽管他算得上是一位数学家,但他更加享受计算机带来的乐趣.用他 ...

  7. hbase 安装笔记

    1.安装 在官方镜像站点下载hbase2.0,地址:https://www.apache.org/dyn/closer.lua/hbase/ 解压tar xzvf hbase-2.0.4-bin.ta ...

  8. 史上最全面,清晰的SharedPreferences解析

    基础用法获取Sp:getput监听器原理分析获取SharedPreferences构造SharedPreferencesgetX原理分析putX原理分析创建editorputStringapplyap ...

  9. 【BZOJ4755】扭动的回文串(Manacher,哈希)

    [BZOJ4755]扭动的回文串(Manacher,哈希) 题面 BZOJ 题解 不要真的以为看见了回文串就是\(PAM,Manacher\)一类就可以过. 这题显然不行啊. 我们主要考虑如何解决跨串 ...

  10. Linux内核分析第一周学习博客 --- 通过反汇编方式学习计算机工作过程

    Linux内核分析第一周学习博客 通过反汇编方式学习计算机工作过程 总结: 通过这次对一个简单C程序的反汇编学习,我了解到计算机在实际工作工程中要涉及大量的跳转指针操作.计算机通常是顺序执行一条一条的 ...