字符串函数
C语言的字符串处理函数
1.puts函数

 //把一个以'\0'结尾的字符串输出到屏幕
 char a[] = "Welcome to";
 char *p = "Linux C Program";
 puts(a);
 puts(p);

2.gets函数

 //从终端输入一个字符数组,返回字符数组的首地址
 ];
 gets(string);
 puts(string);
 //warning: the `gets' function is dangerous and should not be used.
 //系统不推荐使用gets方法了,危险

3.strcpy和strncpy

 #include<string.h>
 char *strcpy(char *dest , char *src);
 char *strncpy(char *dest , char *src ,int n);//复制前n个字符
 //strcpy是string copy缩写,使用这两个函数必须包含string.h,返回值都是dest
 //复制时连同'\0'一起被复制

复制错误代码示范:

 ];
 b = a ;
 //字符串复制只能使用strcpy等类似功能的函数

strcpy不安全,容易被黑客利用,一般用strncpy
示例代码:

 char *s = "hello worlg";
 ],d2[];
 strcpy(d1,s);
 strncpy(d2,s,sizeof(s));
 //
 //strncpy复制不完全。。。

4.strcat 和strncat

 #include<string.h>
 char *strcat(char *dest , char *src);
 char *strncat(char *dest , char *src ,int n);//复制前n个字符
 //把输入的src追加到dest的尾部
 //strcat不安全

5.strcmp    和 strncmp

 #include<string.h>
 char *strcmp(char *s1 , char *s2);//比较两个字符串
 char *strncmp(char *s1 , char *s2 ,int n);//比较前n字符串
 //第一次出现不同字符时,s1-s2的差值为返回值

6.strlen

#include< //返回字符串的实际长度,不会包括结束符'\0',sizeof(s)的计算会包含结束符

7.strlwr 和 strupr//string lower 和string upper的缩写

8.strstr 和 strchr

 #include<string.h>
 char *strstr(char *s1 , char *s2);//s1中寻找s2,返回首次出现指向s2位置的指针,没有找到返回NULL
 char *strchr(char *s1 , char c);//s1寻找c首次出现的位置,返回指针,没有返回NULL
 //----
 #include<stdio.h>
 #include<string.h>

 int main(){
         char *s1 = "Liunx C Program",*s2="unx",*p;

         p = strstr(s1,s2);
         if(p != NULL){
                 printf("%s\n",p);
         }else{
                 printf("not found !");
         }

         p= strchr(s1,'C');
         if(p != NULL){
                 printf("%s\n",p);
         }else{
                 printf("not found!");
         }
         ;
 }

Linux C 程序 字符串函数(12)的更多相关文章

  1. Linux C 程序 字符串运算符-表达式(TWO)

    1.字符串常量 双引号"" :eg:"china"   ,字符串在存储的时候会以一个\0为结束标志.2.符号常量  ,给常量取一个名字. #include< ...

  2. 【Linux C中文函数手册】之 内存和字符串函数

    内存和字符串函数 1) bcmp 比较内存内容 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp表头文件 #include<stri ...

  3. Linux下常用函数-字符串函数

    inux下常用函数-字符串函数 atof(将字符串转换成浮点型数)  相关函数   atoi,atol,strtod,strtol,strtoul 表头文件   #include <stdlib ...

  4. C语言字符串函数例子程序大全 – string相关

    关于字符串函数的应用细则,例子程序 – jerny 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source) ...

  5. 【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

    下面程序演示了在嵌入式Linux和PC机Linux下使用popen函数时,程序的运行结果是有差异的. 两个程序 atest.c 和 btest.c,atest 检查是否有 btest 进程运行,如果没 ...

  6. 前端学PHP之字符串函数

    × 目录 [1]特点 [2]输出 [3]空格[4]大小写[5]HTML[6]格式化[7]比较 前面的话 字符串的处理和分析在任何编程语言中都是一个重要的基础,往往是简单而重要的.信息的分类.解析.存储 ...

  7. Linux应用程序的地址布局

    转载自:http://blog.csdn.net/embedded_hunter http://www.360doc.com/content/12/0405/00/1671317_200882538. ...

  8. gcc 头文件是用户应用程序和函数库之间的桥梁和纽带 功能的真正逻辑实现是以硬件层为基础

    gcc GCC, the GNU Compiler Collection - GNU Project - Free Software Foundation (FSF) http://gcc.gnu.o ...

  9. Linux GDB程序调试工具使用简单介绍

    GDB概述 GDB是GNU开源组织公布的一个强大的UNIX下的程序调试工具.也许,各位比較喜欢那种图形界面方式的,像VC.BCB等IDE的调试,但假设你是在UNIX平台下做软件,你会发现GDB这个调试 ...

随机推荐

  1. 隐藏gvim中的工具栏和菜单栏

    在vim的配置文件.vimrc中添加如下代码: "Toggle Menu and Toolbar set guioptions-=m set guioptions-=T map <si ...

  2. Computer Science Theory for the Information Age-3: 高维空间中的高斯分布和随机投影

    高维空间中的高斯分布和随机投影 (一)在高维球体表面产生均匀分布点的方法 我们来考虑一个采样问题,就是怎样在高维单位球体的表面上均匀的采样.首先,考虑二维的情况,就是在球形的周长上采样.我们考虑如下方 ...

  3. Scala Error: error while loading Suite, Scala signature Suite has wrong version expected: 5.0 found: 4.1 in Suite.class

    准备给scala项目引入单元测试 <dependency> <groupId>org.scalatest</groupId> <artifactId>s ...

  4. BootStrap2学习日记7---表格

    基本表 代码: <div class="container"> <h1 class="page-header">基本表</h1&g ...

  5. Sequence用堆排序

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...

  6. cocos2d-x, protobuf, no config.h, #error "No suitable threading library available."

    在用cocos2d-x3.2 + protobuf编译Android项目的时候,protobuf出现了两个问题: 1. 首先是config.h找不到,查阅自后说是通过命令或工具生成的,里面的内容根据不 ...

  7. css笔记03:伪类first-child

    1. 匹配第一个 <p> 元素 在下面的例子中,选择器匹配作为任何元素的第一个子元素的 p 元素: <html> <head> <style type=&qu ...

  8. 关于 ES6箭头函数

    转自  http://simplyy.space/article/577c5b0dcbe0a3e656c87c24 多个连续的箭头函数与柯里化 高阶函数 高阶函数定义:将函数作为参数或者返回值是函 ...

  9. RMI学习

    前段时间学习JMX,知道可以使用rmi连接器,就顺便看下rmi是什么东西,RMI 全称Remote Method Invocation-远程方法调用,实现远程对象之间的调用,下面原理图来自网络 服务器 ...

  10. Linux系统root用户忘记密码解决方法

    一:在linux系统启动时(如下图),按e键 二:进入到设置页面,定位到如下行: 三:按e键,进入输入界面 四:在编辑行最后面,空格,输入single,回车后回到第二步界面,只是后面多了single ...