C语言用一级指针处理字符串的反思
1.一级指针处理字符串的常见方式
如果使用单个指针,不方便对字符串本身进行操作,只适合遍历。
使用两个指针,
两个指针和字符串大致有两个常见处理方式:
(1)两个指针从字符串首部开始向后移动,同时处理字符串。
(2)两个指针分别指向字符串首部和尾部,向中间靠拢。
2.两个指针放在字符串两端
示例:
去除字符串两端的空格。
#include <ctype.h>
#include <string.h>
#include <stdio.h> void trimSpace(const char *str, char *newStr, int len)
{
const char *p, *q;
int len2; if (NULL == str || NULL == newStr)
return; p = str;
q = p + strlen(str) - ;
while (p < q && isspace(*p)) ++p;
while (p < q && isspace(*q)) --q;
len2 = q - p +;
if (len >= len2) {
memcpy(newStr, p, len2);
newStr[len2] = ;
} else {
memcpy(newStr, p, len);
newStr[len] = ;
} } int main()
{
char str[] = " abcabc ";
char str2[] = {}; trimSpace(str, str2, sizeof(str2) - );
printf("%s\n", str2); return ;
}
(2)字符串翻转
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* 非递归版本,使用指针放在字符串两端 */
void inverse(char *str)
{
char *p, *q, tmp; if (NULL == str)
return; p = str;
q = str + strlen(str) - ;
while (p < q) {
tmp = *p;
*p = *q;
*q = tmp;
++p, --q;
}
} static void _inverse1(char *str1, char *str2)
{
if (!*str2)
return;
_inverse1(str1, str2+);
strncat(str1, str2, );
}
/* 递归版本 */
void inverse1(char *str)
{
char *tmp = NULL;
if (NULL == str)
return;
if ((tmp = (char *)calloc(strlen(str) + , sizeof(char))) == NULL)
return;
_inverse1(tmp, str);
strncpy(str, tmp, strlen(tmp)+);
free(tmp);
} int main()
{
char str[] = ""; inverse1(str);
printf("%s\n", str); return ;
}
3.两个指针放在字符串首部
(1)strstr挖字符串
#include <string.h>
#include <stdio.h> int getCount(const char *str1, const char *str2)
{
const char *p1 = str1, *p2 = str2;
int count = ; if (NULL == str1 || NULL == str2)
return -; while (str1 = strstr(str1, str2)) {
count++;
str1 += strlen(str2);
} return count;
} int main()
{
char str[] = "gsadgdasabcadfaabcasdabc";
char str2[] = "abc";
int n; n = getCount(str, str2);
printf("%d\n", n); return ;
}
(2)去除字符串中的某些字符
去除空格。
old指针的意义是:指向旧的数组,并挖出非空格的字符输出给新数组。
new指针的意义是:指向新的数组,并输入old指针传来的字符。
由于 new 永远不会超过 old 所以,新、旧数组可以使用同样的空间。
#include <ctype.h>
#include <string.h>
#include <stdio.h> void trimSpace(char *str)
{
char *new, *old; if (NULL == str)
return; new = str;
old = str;
while (*old) {
if (isspace(*old))
++old;
else
*new++ = *old++;
}
*new = ;
} int main()
{
char str[] = " 12 34 56 78 "; trimSpace(str);
printf("%s\n", str); return ;
}
4. 总结
如果使用一级指针处理字符串,应该先思考两个一级指针应该放在字符串两端还是一端,然后思考每个指针的实际意义。
C语言用一级指针处理字符串的反思的更多相关文章
- c语言学习笔记 - 指针和字符串
前面学习了字符串是一种字符数组,又知道了指针变量和数组的关系,这里来看一下指针和字符串的关系. #include <stdio.h> int main(void){ char str = ...
- C语言实现二级指针表示字符串数组
头文件: #include<stdlib.h> #include<stdio.h> #include<string.h> 函数原型: char ** createB ...
- C语言学习笔记--指针与字符串
字符类型 char(character)是一种整数,也是一种特殊的类型:字符.这是因为 ① 用单引号表示的字符字符字面量:‘a’,'1' ②‘’也是一个字符 ③printf和scanf里用%c来输入. ...
- C语言复习:字符串和一级指针
字符串基本操作 字符数组初始化方法 int main() { //1 {}号法 初始化列表 //数组初始化有2种方法 默认元素个数.指定元素个数 char buf1[] = { ...
- Swift3.0语言教程使用指针创建和初始化字符串
Swift3.0语言教程使用指针创建和初始化字符串 Swift3.0语言教程使用指针创建和初始化字符串苹果的Swift团队花了不少功夫来支持C的一些基础特性.C语言中为我们提供了指针,Swift也不例 ...
- [C语言]进阶|指针与字符串
------------------------------------------------------------------------------------ 回顾:[C语言]指针与字符串 ...
- 程序设计入门-C语言基础知识-翁恺-第七周:指针与字符串-详细笔记(七)
目录 第七周:指针与字符串 7.1 指针初步 7.2 字符类型 7.3 字符串 7.3 课后练习 第七周:指针与字符串 7.1 指针初步 sizeof 是一个运算符,给出某个类型或变量在内存中所占据的 ...
- 【C语言】-指针和字符串
本文目录 字符串回顾 一.用指针遍历字符串的所有字符 二.用指针直接指向字符串 三.指针处理字符串的注意 说明:这个C语言专题,是学习iOS开发的前奏.也为了让有面向对象语言开发经验的程序员,能够快速 ...
- C语言指针系列 - 一级指针.一维数组,二级指针,二维数组,指针数组,数组指针,函数指针,指针函数
1. 数组名 C语言中的数组名是一个特殊的存在, 从本质上来讲, 数组名是一个地址, 我们可以打印一个指针的值,和打印一个数组的值来观察出这个本质: int nArray[10] ={ 0 }; in ...
随机推荐
- [翻译] HTKDragAndDropCollectionViewLayout
HTKDragAndDropCollectionViewLayout Custom UICollectionViewLayout that works together with a custom U ...
- haproxy开启日志功能
haproxy在默认情况不会记录日志,除了在haproxy.conf中的global段指定日志的输出外,还需要配置系统日志的配置文件.下面以centos6.4为例,haproxy使用系统自带的rpm报 ...
- 月报 提取/保存 到OneDrive. 并发送反馈邮件
- August 02nd 2017 Week 31st Wednesday
Love means never having to say you are sorry. 爱就是永远不必说对不起. If there is ture love, you will never do ...
- fzu_oop_east 第一次作业
第一题 题目: 代码: #include<iostream> #include<cstdio> using namespace std; class Date { public ...
- 关于$.fn.scrollPath is not a function
关于$.fn.scrollPath is not a function 在做项目过程中,用到了一个jQuery的滚动路径插件——jQuery Scroll Path.引入相关的js文件后,但是控制台一 ...
- unittest:1 用例编写
unittest是python自带的单元测试框架,包含测试用例case,测试集suite,测试集加载loader,测试执行runner,测试结果result等. 简单使用:写一个用例类继承自unitt ...
- 二值形态学——腐蚀与膨胀 及 C语言代码实现
参考文献:数字图像处理(第三版) 何东健 西安电子科技大学出版社 二值形态学中的运算对象是集合, 但实际运算中, 当涉及两个集合时并不把它们看作是互相对等的. 一般设A为图像集合, S为结构元素, 数 ...
- Protocols, Generics, and Existential Containers — Wait What?
For the longest time now, I thought that the two functions above were the same. But in actuality, wh ...
- 5、RabbitMQ-订阅模式 Publish/Subscribe
http://www.rabbitmq.com/tutorials/tutorial-three-java.html 1.模型图 我们之前学习的都是一个消息只能被一个消费者消费,那么如果我想发一个消息 ...