由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里

strlen()

获取字符串长度,成功则返回字符串 string 的长度;如果 string 为空,则返回 0。

#include<stdio.h>
#include<stdlib.h>
#define N 1000
int count = ; int strlen(char *str)
{
int num = ; //定义一个计数器
while('\0' != *str++)
{
num++;
}
return num;
} void test(char *str)
{
printf("所要测试的字符串为: %s\n",str);
count = strlen(str); //调用函数
printf("所输入的字符串长度为:%d\n\n",count);
} void main()
{
char str1[] = "hello world!"; //这样的赋值方式会有在尾部自动一个'\0'
char *str2 = "hello world!"; //这样的赋值方式会有在尾部自动一个'\0'
char str3[] = "world hello!"; //这样的赋值方式会在剩余的位置全部自动添加'\0'
char str4[N] = {};
test(str1);
test(str2);
test(str3); printf("请输入所要测试的数组:\n");
gets(str4); //此函数会在最后添加NULL字符 即'\0'
test(str4);
system("pause");
}

strcpy()

head.h

#include<stdio.h>
#include<string.h>
#define N 100
void strcpy1(char *str_cpy, char const *str);

_strcpy().c

#include"head.h"

void strcpy1(char *str_cpy,char const *str)  //为了保证主数组的只读性,所以加"const"修饰
{
while(*str != '\0')
{
*str_cpy = *str ;
str_cpy ++;
str++;
}
*str_cpy = '\0'; //添加结束符
}

main.c

#include"head.h"

void main()
{
char str[N];
char str_cpy[N] ;
printf("请输入所要主字符串数组:\n");
scanf("%s",&str); strcpy1(str_cpy,str); //复制
printf("复制前的主字符串为的%s\n",str);
printf("复制后新字符串为的%s\n",str_cpy); getchar();
getchar();
}

explode()

#include<stdio.h>
#include<stdlib.h> #define N 50
char *ptr[] = {}; //定义指针数组,用来保存分割后的子串
char temp[N] = {}; //定义临时字符数组,用来临时保存分割后的子串 void explode(char *str,char c) //char c 类型用来保存分隔符
{
int i = ;
while((*str) != c)
{
temp[i] = *str; //指针中的字符串赋值给临时数组temp
str++;
i++;
}
str++; //此时 (*str) 是 分割符 c,自增1跳过分隔符
temp[i] = '\0'; //将分割的子串添加结束符号 '\0'
printf("%s\n",temp); //将分割后的子串打印出来
i = ; // 将i置零,方便下次使用临时数
while((*str) != '\0')
{ temp[i] = *str; //指针中的字符串赋值给临时数组temp
str++;
i++;
} str++; //此时 (*str) 是 结束符,自增1跳过结束符 temp[i] = '\0'; //将分割的子串添加结束符号 '\0' printf("%s\n",temp); //将分割后的子串打印出来 i = ; // 将i置零,方便下次使用临时数 return ;
} void main()
{
char *str = "hello,world!";
char b[N] = {} ; //用于保存用户所输入的数组
char c; //用于保存用户所输入的分割符 printf("下面用'hello,world!'作为示例,其中分割符为','\n");
printf("下面是使用分割函数explode(hello,world!,'c')的结果\n"); explode(str,','); //此函数不局限于 ',' 作为分隔符 ,可以用其他符号 printf("请输入只有两个子字符串的字符串:例如hello,world!\n");
scanf("%s",b);
printf("请输入你想用的分隔符:\n"); getchar(); //缓冲点回车键,不然会以回车键作为分隔符,程序崩溃
scanf("%c",&c); explode(b,c); //此函数不局限于 ',' 作为分隔符 ,可以用其他符号 system("pause");
}

最新文章

  1. java多线程-锁
  2. MFC下OpenGL入门(可以用)
  3. OD使用教程12
  4. unbutu下搭建FTP服务
  5. C语言扩展Python模块
  6. 理解C#中的闭包
  7. C# socket 实现消息中心向消息平台 转发消息
  8. 解决Android singleTask模式下PendingIntent不能给onNewIntent传值的Bug
  9. c++ primer (5)2
  10. Java Memory Management(1)
  11. lesson3:使用java代码的方式对不能识别的协议进行压力测试
  12. 关于Oracle连接超时的问题
  13. 用AngularJS实现对表格的增删改查(仅限前端)
  14. web前端技术学习
  15. Java学习之JDBC 2019/3/10
  16. JavaScript——变量
  17. mouseover和mouseout事件的相关元素
  18. github高效搜索使用总结
  19. nginx的日志分析
  20. FunDA(4)- 数据流内容控制:Stream data element control

热门文章

  1. 用VB6.0实现串口通信
  2. parentNode,parentElement,offsetParent
  3. JDK静态代理示例代码
  4. zookeeper简绍
  5. 记一次为gitlab启用CI的过程
  6. TColor转化为字符串
  7. 基于jquery-ui及bootstrap的可拖拽模态框
  8. 更改Mysql数据库数据存储位置的具体步骤
  9. springmvc web.xml配置之 -- ContextLoaderListener
  10. kafka常用运维命令