C 字符串操作函数

针对C风格的字符串(char p[n];):

长度(strlen)、追加(strcat, strncat)、比较(strcmp, strncmp)、查找(strchr, strstr)等。

  --带n的版本,是有限操作,而非全部操作。例如strcmp 是对比两个字符串,而strncmp 则是对比两个字符串的前n个字符。

  --追加应该使用strncat,因为strcat 有溢出的危险。(奇怪的是,strcat 在我的minGW32 - gcc 4.9.3这里没有提示错误。)

输入(scanfgetsfgetssscanf)、输出(printfputsfputssprintf)。

  --scanf 也存在溢出的可能,且会将空白(空格、回车等)当成输入终止。

  --gets 只会将回车当成输入终止。但它也存在溢出可能。

  --fgets 是安全的。

  --sscanf 是将字符串中的内容读取到指定的变量中!和 sprintf 相反。

  --sprintf 则是将一串内容输出到指定的字符串!!

还有一个 atoi 函数,可以将字符串转成int 。(atof--转成浮点数,其他略)

  --a 应该是 char array的意思。

注意:上面说的溢出,都是目标字符数组的长度 无法容纳最终的结果导致的。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    char s1[15]="hello world";
    char s2[15]="abc defxxx";

    strcat(s1, s2); //追加
    // strncat(s1, s2, strlen(s2));
    printf("%s\n", s1);

    int v = strcmp(s1, s2); //比较
    printf("%d\n", v);

    char s3[]="hello";
    char s4[]="hello";
    if( s3 != s4){ //应该比较的是地址
        printf("%s != %s\n", s3, s4);
    }

    char s5[]="helloaaa";
    char s6[]="hellobbb";
    v = strncmp(s5, s6, 5); //比较前5个字符
    printf("%d\n", v);

    strcpy(s5, s6);
    printf("%s, %s\n", s5, s6);

    strncpy(s1, s2, 3);//拷贝前几位
    printf("%s, %s\n", s1, s2);

    int i = 200;
    char ss[100]={0};
    sprintf(ss, "i = %d", i);//把要输出的内容输出到string中?
    printf("%s\n", ss);

    // ss=itoa(i); //不是标准的c语言库函数。vs有
    //atoi是标准的c语言库函数

    char s7[100]="abc=500";
    char s8[100]="5+6=";
    sscanf(s7, "abc=%d", &i); //利用字符串作为输入,输出指定内容到指定变量。
    printf("%d\n", i);
    int a,b;
    sscanf(s8, "%d+%d=", &a, &b);
    printf("%d, %d\n", a, b);

    const char *p = strchr(s7, ‘=‘); //查找字符
    printf("%s\n", p);
    p = strstr(s7, "b"); //查找字符串
    printf("%s\n", p);

    strcpy(s7, "abc_123_ee_xx");
    p=strtok(s7, "_"); //将_替换成0
    // printf("%s, %p, %p\n", p, p, s7);
    // p=strtok(NULL, "_"); //怎么知道是对哪个操作的????
    // printf("%s, %p, %p\n", p, p, s7);
    // p=strtok(NULL, "_"); //怎么知道是对哪个操作的????
    // printf("%s, %p, %p\n", p, p, s7);
    while(p){
        printf("%s, %p, %p\n", p, p, s7);
        p=strtok(NULL, "_");
    }

    return 0;
}
时间: 2024-12-21 07:12:21

C 字符串操作函数的相关文章

C语言的常用字符串操作函数(一)

一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这样丰富而实用,在此记录,已备后用. No.1 strlen():字符串长度计算函数 应用实例: 1 #include<stdio.h> 2 #include<string.h> 3 4 char TextBuff[] = "Hello_My_Friend!"; 5

C/C++ 字符串操作函数 思维导图梳理

这些常用的字符串操作函数都是包在string.h头文件中. 分享此图,方便大家记忆 <(^-^)> 选中图片点击右键,在新标签页中打开图片会更清晰

转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文

转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 作者:jcsu C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. 字符串转化为整数 - atoi4. 字符串求长 - strlen5. 字符串连接 - strcat6. 字符串比较 - strcmp7. 计算字符串中的元音字符个数8. 判断一个字符串是否是回文1. 写一个函数实现字符串反转 版本1 - while版 void strRev(char *s){    

windows平台没有提供的两个字符串操作函数。

在看一些开源代码时,经常看到一些字符串操作函数,这些函数在Linux平台下是有的,但在windows平台上,MS没有提供.因此在软件中不得不自己实现.常见的库函数有: //获得当前字符的一个拷贝,由外部释放内存. char *strdup(const char *src) { int len; char *dst; len = strlen(src) + 1; if ((dst = (char *) malloc(len)) == NULL) return (NULL); strcpy(dst,

mysql常用字符串操作函数大全

测试表 CREATE TABLE `string_test` ( `id` int(11) NOT NULL auto_increment COMMENT '用户ID', `name` varchar(50) NOT NULL default '' COMMENT '名称', `job` varchar(23) NOT NULL COMMENT '工作', `sex` tinyint(1) NOT NULL default '1' COMMENT '性别', `hobby` varchar(10

【C语言】 字符串操作函数及内存拷贝函数归总

今天在这里把零散的一些常用的字符串操作函数和内存拷贝函数进行一下归总实现. 一 . 字符串操作函数 字符串操作函数有很多,这里我列举一些常用的函数,以及自实现的代码: 字符串拷贝函数: 函数原型: char* my_strcpy(char* dst,const char* src) strcpy(): char* my_strcpy(char* dst,const char* src) {     assert(dst);     assert(src);     char *ret = dst

mysql常用字符串操作函数大全,以及实例

今天在论坛中看到一个关于mysql的问题,问题如下 good_id     cat_id12654         665,56912655         601,4722 goods_id是商品idcat_id是分类id当我,怎么根据这种分类ID查数据(一个商品有多个分类,而且用逗号隔开了)我现在用的是like 这样的话,输入一个分类id是688,或者4722都能出来这个商品,但输入一个722也出来这个商品了. 如果用like做的话,肯定会有问题的,我的开始的想法是,把cat_id里面的字符

jQuery 工具类函数-字符串操作函数

调用名为$.trim的工具函数,能删除字符串中左右两边的空格符,但该函数不能删除字符串中间的空格,调用格式为: $.trim (str); 参数str表示需要删除左右两边空格符的字符串. <body> <div id="divtest"> <div class="title"> <span class="fl">字符串操作函数</span> <span class="fr

LoadRunner中常用的字符串操作函数

LoadRunner中常用的字符串操作函数有:                strcpy(destination_string, source_string);               strcat(string_that_gets_appended, string_that_is_appended);51Testing软件测试网:J3~c:c[(wR%A2l               atoi(string_to_convert_to_int); //returns the integ

Sql字符串操作函数

1.去空格函数 (1).LTRIM() 把字符串头部的空格去掉. (2).RTRIM() 把字符串尾部的空格去掉. 2.字符转换函数(1).ASCII()返回字符表达式最左端字符的ASCII 码值.在ASCII()函数中,纯数字的字符串可不用‘’括起来,但含其它字符的字符串必须用‘’括起来使用,否则会出错.(2).CHAR()将ASCII 码转换为字符.如果没有输入0 ~ 255 之间的ASCII 码值,CHAR() 返回NULL .(3).LOWER()和UPPER()LOWER()将字符串全