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 ...
随机推荐
- [翻译] AJProgressPanel
AJProgressPanel Animated progress panel 可做动画的进度条 No images needed, all CoreGraphics code 不需要图片,使用Cor ...
- 使用HVTableView动态展开tableView中的cell
使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTab ...
- composer 应用【Modern PHP】
目录 安装(linux) composer.lock 文件 composer.josn 文件 自动加载PHP组件 组件包库地址 实例 composer私有仓库 composer 遵循PSR准则,解决安 ...
- cetnos7下openresty使用luarocks 进行lua的包管理
先安装一下包管理工具 yum install luarocks lua-devel -y luarocks install lpack ln -s /usr/lib64/lua /usr/local/ ...
- 深入浅出SharePoint——使用WinDbg进行调试
- [T-ARA][SEXY LOVE]
歌词来源:http://music.163.com/#/song?id=22704402 作曲 : 新沙洞老虎/崔圭成 [作曲 : 新沙洞老虎/崔圭成] [作曲 : 新沙洞老虎/崔圭成] 作词 : 新 ...
- 36、XmlReader与 XMLWriter(抽象类)
一.概述 XMLReader为抽象类,其派生类有:XmlDictionaryReader.XmlNodeReader.XmlTextReader(与IO命名空间中的TextReader对象一起使用). ...
- Salesforce平台支持多租户Multi tenant的核心设计思路
Multitenancy is the fundamental technology that clouds use to share IT resources cost-efficiently an ...
- [T-ARA][Cry Cry]
歌词来源: Cry Cry:http://music.163.com/#/song?id=22704434 Cry Cry (Ballad Ver.):http://music.163.com/#/s ...
- .Net Intelligencia.UrlRewriter 重定向参数中文支持配置方法
在使用.Net 官方 Url重定向组件时,发现若原地址包含中文,如:http://localhost/首页.html 重定向为:http://localhost/index.aspx?id=首页 时 ...