day4_字符串的两头堵模型
来源:互联网 发布:贝尔野外求生刀数据 编辑:程序博客网 时间:2024/05/17 06:35
一、求非空格的字符串长度
1、方法一 普通函数
//方法一int main01(){ //求非空格的字符串长度 char *p = " abcdefgg "; int i = 0; int j = 0; j = strlen(p) - 1; int count = 0; while(isspace(p[i]) && p[i] != '\0') { i++; } while(isspace(p[j]) && p[j] != '\0') { j--; } count = j - i + 1; printf("count:%d \n",count); printf("Hello world!\n"); system("pause"); return 0;}
2、方法二 构造API函数
//求非空格的字符串长度//方法二 API函数void getCount(char *str,int *Pcount){ char *p = str; int i = 0; int j = 0; j = strlen(p) - 1; int count = 0; if(str == NULL && Pcount == NULL) { return; } while(isspace(p[i]) && p[i] != '\0') { i++; } while(isspace(p[j]) && p[j] != '\0') { j--; } count = j - i + 1; *Pcount = count; printf("count:%d \n",*Pcount); return 0;}
3、方法三 去除字符串前后的空格并返回新串
//去除字符串前后的空格并返回新串//方法三int trimSpace(char *str,char *newstr){ char *p = str; int i = 0; int j = 0; j = strlen(p) - 1; int count = 0; if(str == NULL && newstr == NULL) { return; } while(isspace(p[i]) && p[i] != '\0') { i++; } while(isspace(p[j]) && p[j] != '\0') { j--; } count = j - i + 1; strncpy(newstr,str+i,count); newstr[count] = '\0'; return 0;}int main02(){ char *p = " abcdefgg "; char buf[1024] = {0}; int getcount = 0; getCount(p,&getcount); printf("getcount:%d \n",getcount); trimSpace(p,buf); printf("buf:%s \n",buf); printf("Hello world!\n"); system("pause"); return 0;}
4、方法四 典型错误及修改原空间
//去除字符串前后的空格并返回新串//方法四//典型错误 不能修改全局区的字符串//修改 将字符串写到字符串数组里面 修改栈区的数据int trimSpace02(char *str){ char *p = str; int i = 0; int j = 0; j = strlen(p) - 1; int count = 0; if(str == NULL) { return -1; } while(isspace(p[i]) && p[i] != '\0') { i++; } while(isspace(p[j]) && p[j] != '\0') { j--; } count = j - i + 1; strncpy(str,str+i,count);//error 不能向str所指向的内存空间写入数据 str[count] = '\0'; return 0;}int main(){// char *p = " abcdefgg ";//数据在全局区里面 不能被修改 char buf[1024] = " abcdefgg "; int getcount = 0; trimSpace02(buf);// printf("p:%s \n",p); printf("buf:%s \n",buf); printf("Hello world!\n"); system("pause"); return 0;}
阅读全文
0 0
- day4_字符串的两头堵模型
- 字符串中挖字符串,两头堵模型
- C语言常用的两头堵模型
- 两头堵模型
- 两头堵模型
- 统计字符串中非空格字符串的长度并取出--两头堵模型
- 【C语言提高19】【字符串模型】两头堵模型
- C语言的项目开发模型(1)-字符串两头堵
- 【C语言提高20】字符串反转(两头堵的变形)
- 两头堵模型记录字符数
- 两头堵模型取中间字符
- “两头堵模型”计数+取字符
- C语言两头堵模型(去两头空格)
- 字符串两头堵的模型就是去掉两边空格,已经字符串1,在字符串2出现的次数,不修改输入。
- 两头堵模型:有一个字符串开头或结尾有n个空格,欲去掉前后空格返回一个新的字符串
- C字符串操作:两头堵实现trim()
- day4_字符串的相关库函数
- day4_二级指针的内存模型
- centos6.5 ifconfig -a显示为eth1,但是配置文件只有eth0
- 练习
- jquery中绑定事件(on,bind,delegate,live)详解
- TWS API 97.3 python3.6 出现 decode错误
- springmvc源码分析系列-请求处理流程
- day4_字符串的两头堵模型
- 重新运行explorer.exe
- obs之lame编码
- 【读书笔记】Android源码设计模式解析与实战(三)——建造者模式(Builder)
- git分支(branch)操作相关命令
- 文件下载中文文件名不显示
- Linux下查看文件内容的命令
- node.js——麻将算法(四)胡牌算法的一些优化处理方案(无赖子版)
- Axis2开发WebService客户端 的3种方式