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语言用一级指针处理字符串的反思的更多相关文章

  1. c语言学习笔记 - 指针和字符串

    前面学习了字符串是一种字符数组,又知道了指针变量和数组的关系,这里来看一下指针和字符串的关系. #include <stdio.h> int main(void){ char str = ...

  2. C语言实现二级指针表示字符串数组

    头文件: #include<stdlib.h> #include<stdio.h> #include<string.h> 函数原型: char ** createB ...

  3. C语言学习笔记--指针与字符串

    字符类型 char(character)是一种整数,也是一种特殊的类型:字符.这是因为 ① 用单引号表示的字符字符字面量:‘a’,'1' ②‘’也是一个字符 ③printf和scanf里用%c来输入. ...

  4. C语言复习:字符串和一级指针

    字符串基本操作 字符数组初始化方法 int main() {     //1 {}号法 初始化列表     //数组初始化有2种方法 默认元素个数.指定元素个数     char buf1[] = { ...

  5. Swift3.0语言教程使用指针创建和初始化字符串

    Swift3.0语言教程使用指针创建和初始化字符串 Swift3.0语言教程使用指针创建和初始化字符串苹果的Swift团队花了不少功夫来支持C的一些基础特性.C语言中为我们提供了指针,Swift也不例 ...

  6. [C语言]进阶|指针与字符串

    ------------------------------------------------------------------------------------ 回顾:[C语言]指针与字符串 ...

  7. 程序设计入门-C语言基础知识-翁恺-第七周:指针与字符串-详细笔记(七)

    目录 第七周:指针与字符串 7.1 指针初步 7.2 字符类型 7.3 字符串 7.3 课后练习 第七周:指针与字符串 7.1 指针初步 sizeof 是一个运算符,给出某个类型或变量在内存中所占据的 ...

  8. 【C语言】-指针和字符串

    本文目录 字符串回顾 一.用指针遍历字符串的所有字符 二.用指针直接指向字符串 三.指针处理字符串的注意 说明:这个C语言专题,是学习iOS开发的前奏.也为了让有面向对象语言开发经验的程序员,能够快速 ...

  9. C语言指针系列 - 一级指针.一维数组,二级指针,二维数组,指针数组,数组指针,函数指针,指针函数

    1. 数组名 C语言中的数组名是一个特殊的存在, 从本质上来讲, 数组名是一个地址, 我们可以打印一个指针的值,和打印一个数组的值来观察出这个本质: int nArray[10] ={ 0 }; in ...

随机推荐

  1. Python初学者第十七天 函数(1)

    17day 函数 1.函数定义: 函数 是指将一组语句的集合通过一个名字(函数名)封装起来,想要执行这个函数,只需调用其函数名即可 2.函数的特性: a 减少重复代码 b 使程序变得可扩展 c 使程序 ...

  2. codeforces 666C Codeword

    codeforces 666C Codeword 题意 q个询问,一种询问是给你一个字符串,还有一种是问长度为n的,包含当前字符串为子序列的字符串有多少个. 题解 容易写出式子,但是不好化简. 观察一 ...

  3. PhoneGap API 之事件处理_双击返回键退出程序

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. vue2.* 双向数据绑定 Vue事件介绍 以及Vue中的ref获取dom节点 04

    <template> <div id="app"> <!-- 双向数据绑定(必须在表单里面使用) m:model v:view mvvm:model改 ...

  5. 7、RabbitMQ-主题模式

    1.模式图 发送到主题交换的消息不能具有任意的 routing_key - 它必须是由点分隔的单词列表. 单词可以是任何内容,但通常它们指定与消息相关的一些功能.一些有效的路由键示例:“ stock. ...

  6. git编译安装报错 http-push.c:20:19: 警告:expat.h:没有那个文件或目录

    解决: [root@hdoop3 git-2.18.1]# yum install expat-devel

  7. Java 数据库操作oracle增删改查,通用封装基于hashmap

    pt1:首先安装oracle连接驱动 下载地址:https://pan.baidu.com/s/1jW_ofgU4eJmAn7Y2J5B46A  密码:epkz 1.将ojdbc6.jar导入项目中 ...

  8. Java Runnable和Thread区别

    Thread是多个线程分别完成自己的任务,Runnable是多个线程共同完成1个任务.在实际开发中,一个多线程的操作很少使用Thread类,而是通过Runnable接口完成,好处有: 1. 避免点继承 ...

  9. myeclipse2014黑色主题风格设置

    http://jingyan.baidu.com/article/915fc41494db8451384b2043.html?st=2&os=0&bd_page_type=1& ...

  10. GitHub学生认证示范

    打开网址:https://education.github.com/ 点击Get Your Pack 点击I am  a Student 填写资料上传学生证照片,等待通过,如果没有GitHub账号就注 ...