字符串函数是最问常用的库函数之一,本文整理了常用的字符串函数,其来源为互联网

函数名: stpcpy

功 能: 拷贝一个字符串到另一个
用 法: char *stpcpy(char *destin, char *source);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{char string[10];char *str1 = "abcdefghi";stpcpy(string, str1);printf("%sn", string);return 0;
}

函数名: strcat

功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char *source);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{char destination[25];char *blank = " ", *c = "C++", *Borland = "Borland";strcpy(destination, Borland);strcat(destination, blank);strcat(destination, c);printf("%sn", destination);return 0;
}

函数名: strchr

功 能: 在一个串中查找给定字符的第一个匹配之处
用 法: char *strchr(char *str, char c);
程序例:

#include <string.h>
#include <stdio.h>
int main(void){char string[15];char *ptr, c = 'r';strcpy(string, "This is a string");ptr = strchr(string, c);if (ptr)printf("The character %c is at position: %dn", c, ptr-string);elseprintf("The character was not foundn");return 0;}

函数名: strcmp

功 能: 串比较
用 法: int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0
程序例:

#include <string.h>
#include <stdio.h>
int main(void) { char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc"; int ptr; ptr = strcmp(buf2, buf1); if (ptr > 0) printf("buffer 2 is greater than buffer 1n"); else printf("buffer 2 is less than buffer 1n"); ptr = strcmp(buf2, buf3); if (ptr > 0) printf("buffer 2 is greater than buffer 3n"); else printf("buffer 2 is less than buffer 3n"); return 0; }

函数名: strncmpi

功 能: 将一个串中的一部分与另一个串比较, 不管大小写
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{ char *buf1 = "BBB", *buf2 = "bbb"; int ptr; ptr = strcmpi(buf2, buf1); if (ptr > 0) printf("buffer 2 is greater than buffer 1n"); if (ptr < 0) printf("buffer 2 is less than buffer 1n"); if (ptr == 0) printf("buffer 2 equals buffer 1n"); return 0;
}

函数名: strcpy

功 能: 串拷贝
用 法: char *strcpy(char *str1, char *str2);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{ char string[10]; char *str1 = "abcdefghi"; strcpy(string, str1); printf("%sn", string); return 0;
}

函数名: strcspn

功 能: 在串中查找第一个给定字符集内容的段
用 法: int strcspn(char *str1, char *str2);
程序例:

#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{ char *string1 = "1234567890"; char *string2 = "747DC8"; int length; length = strcspn(string1, string2); printf("Character where strings intersect is at position %dn", length); return 0;
}

函数名: strdup

功 能: 将串拷贝到新建的位置处
用 法: char *strdup(char *str);
程序例:

#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{ char *dup_str, *string = "abcde"; dup_str = strdup(string); printf("%sn", dup_str); free(dup_str); return 0;
}

函数名: stricmp

功 能: 以大小写不敏感方式比较两个串
用 法: int stricmp(char *str1, char *str2);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{ char *buf1 = "BBB", *buf2 = "bbb"; int ptr; ptr = stricmp(buf2, buf1); if (ptr > 0) printf("buffer 2 is greater than buffer 1n"); if (ptr < 0) printf("buffer 2 is less than buffer 1n"); if (ptr == 0) printf("buffer 2 equals buffer 1n"); return 0;
}

函数名: strerror

功 能: 返回指向错误信息字符串的指针
用 法: char *strerror(int errnum);
程序例:

#include <stdio.h>
#include <errno.h>
int main(void)
{ char *buffer; buffer = strerror(errno); printf("Error: %sn", buffer); return 0;
}

函数名: strcmpi

功 能: 将一个串与另一个比较, 不管大小写
用 法: int strcmpi(char *str1, char *str2);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{ char *buf1 = "BBB", *buf2 = "bbb"; int ptr; ptr = strcmpi(buf2, buf1); if (ptr > 0) printf("buffer 2 is greater than buffer 1n"); if (ptr < 0) printf("buffer 2 is less than buffer 1n"); if (ptr == 0) printf("buffer 2 equals buffer 1n"); return 0;
}

函数名: strncmp

功 能: 串比较
用 法: int strncmp(char *str1, char *str2, int maxlen);
程序例:

#include <string.h>
#include <stdio.h>
int  main(void)
{ char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; int ptr; ptr = strncmp(buf2,buf1,3); if (ptr > 0) printf("buffer 2 is greater than buffer 1n"); else printf("buffer 2 is less than buffer 1n"); ptr = strncmp(buf2,buf3,3); if (ptr > 0) printf("buffer 2 is greater than buffer 3n"); else printf("buffer 2 is less than buffer 3n"); return(0);
}

函数名: strncmpi

功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写
用 法: int strncmpi(char *str1, char *str2);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{ char *buf1 = "BBBccc", *buf2 = "bbbccc"; int ptr; ptr = strncmpi(buf2,buf1,3); if (ptr > 0) printf("buffer 2 is greater than buffer 1n"); if (ptr < 0) printf("buffer 2 is less than buffer 1n"); if (ptr == 0) printf("buffer 2 equals buffer 1n"); return 0;
}

函数名: strncpy

功 能: 串拷贝
用 法: char *strncpy(char *destin, char *source, int maxlen);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{ char string[10]; char *str1 = "abcdefghi"; strncpy(string, str1, 3); string[3] = ''; printf("%sn", string); return 0;
}

函数名: strnicmp

功 能: 不注重大小写地比较两个串
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{ char *buf1 = "BBBccc", *buf2 = "bbbccc"; int ptr; ptr = strnicmp(buf2, buf1, 3); if (ptr > 0) printf("buffer 2 is greater than buffer 1n"); if (ptr < 0) printf("buffer 2 is less than buffer 1n"); if (ptr == 0) printf("buffer 2 equals buffer 1n"); return 0;
}

函数名: strnset

功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strnset(char *str, char ch, unsigned n);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{ char *string = "abcdefghijklmnopqrstuvwxyz"; char letter = 'x'; printf("string before strnset: %sn", string); strnset(string, letter, 13); printf("string after  strnset: %sn", string); return 0;
}

函数名: strpbrk

功 能: 在串中查找给定字符集中的字符
用 法: char *strpbrk(char *str1, char *str2);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{ char *string1 = "abcdefghijklmnopqrstuvwxyz"; char *string2 = "onm"; char *ptr; ptr = strpbrk(string1, string2); if (ptr) printf("strpbrk found first character: %cn", *ptr); else printf("strpbrk didn't find character in setn"); return 0;
}

函数名: strrchr

功 能: 在串中查找指定字符的最后一个出现
用 法: char *strrchr(char *str, char c);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{ char string[15]; char *ptr, c = 'r'; strcpy(string, "This is a string"); ptr = strrchr(string, c); if (ptr) printf("The character %c is at position: %dn", c, ptr-string); else printf("The character was not foundn"); return 0;
}

函数名: strrev

功 能: 串倒转
用 法: char *strrev(char *str);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{ char *forward = "string"; printf("Before strrev(): %sn", forward); strrev(forward); printf("After strrev():  %sn", forward); return 0;
}

函数名: strset

功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strset(char *str, char c);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{ char string[10] = "123456789"; char symbol = 'c'; printf("Before strset(): %sn", string); strset(string, symbol); printf("After strset():  %sn", string); return 0;
}

函数名: strspn

功 能: 在串中查找指定字符集的子集的第一次出现
用 法: int strspn(char *str1, char *str2);
程序例:

#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{ char *string1 = "1234567890"; char *string2 = "123DC8"; int length; length = strspn(string1, string2); printf("Character where strings differ is at position %dn", length); return 0;
}

函数名: strstr

功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{ char *str1 = "Borland International", *str2 = "nation", *ptr; ptr = strstr(str1, str2); printf("The substring is: %sn", ptr); return 0;
}

函数名: strtod

功 能: 将字符串转换为double型值
用 法: double strtod(char *str, char **endptr);
程序例:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{ char input[80], *endptr; double value; printf("Enter a floating point number:"); gets(input); value = strtod(input, &endptr); printf("The string is %s the number is %lfn", input, value); return 0;
}

函数名: strtok

功 能: 查找由在第二个串中指定的分界符分隔开的单词
用 法: char *strtok(char *str1, char *str2);
程序例:

#include <string.h>
#include <stdio.h>
int main(void)
{ char input[16] = "abc,d"; char *p; /* strtok places a NULL terminator in front of the token, if found */ p = strtok(input, ","); if (p)   printf("%sn", p); /* A second call to strtok using a NULL as the first parameter returns a pointer to the character following the token  */ p = strtok(NULL, ","); if (p)   printf("%sn", p); return 0;
}

函数名: strtol

功 能: 将串转换为长整数
用 法: long strtol(char *str, char **endptr, int base);
程序例:

#include <stdlib.h>
#include <stdio.h>
int main(void)
{ char *string = "87654321", *endptr; long lnumber; /* strtol converts string to long integer  */ lnumber = strtol(string, &endptr, 10); printf("string = %s  long = %ldn", string, lnumber); return 0;
}

函数名: strupr

功 能: 将串中的小写字母转换为大写字母
用 法: char *strupr(char *str);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{ char *string = "abcdefghijklmnopqrstuvwxyz", *ptr; /* converts string to upper case characters */ ptr = strupr(string); printf("%sn", ptr); return 0;
}

函数名: swab

功 能: 交换字节
用 法: void swab (char *from, char *to, int nbytes);
程序例:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char source[15] = "rFna koBlrna d";
char target[15];
int main(void)
{ swab(source, target, strlen(source)); printf("This is target: %sn", target); return 0;
}

isalpha()

PS:isalpha()是字符函数,不是字符串函数
原型:extern int isalpha(int c);
用法:#include <ctype.h>
功能:判断字符c是否为英文字母
说明:当c为英文字母a-z或A-Z时,返回非零值,否则返回零。
举例:

#include <syslib.h>
#include <ctype.h>
#include <stdio.h>main()
{int c;clrscr();        // clear screenprintf("Press a key");for(;;){c=getchar();clrscr();printf("%c: %s letter",c,isalpha(c)?"is":"not");}return 0; // just to avoid warnings by compiler
}

转载于:https://www.cnblogs.com/cj5785/p/10664755.html

C学习笔记-字符串处理函数相关推荐

  1. C语言学习笔记---字符串拼接函数 strcat() 和 strncat()

    strcat()函数    strcat()函数主要用来拼接字符串,用于将一个字符串拼接到另一个字符串的后面.下面通过一个简单的例子来演示一下这个函数的使用方法. int main() {char s ...

  2. C语言学习笔记---字符串转换函数

    字符串转整数   字符串转换为整数的函数有两个,他们的函数原型如下: int __cdecl atoi(const char *_Str);long __cdecl atol(const char * ...

  3. C语言学习笔记---字符串对比函数strspn()和strcspn()函数

    strspn()函数   如果要对比两个字符串中从第一个字符开始总共有多少个相同字符时,可以使用strspn()函数,它里面有两个参数字符串1和字符串2,从字符串1开始位置依次对比字符串1和字符串2有 ...

  4. C语言学习笔记---字符串查找函数strstr()和strpbrk()函数

    strstr()函数   C语言中如何向查找一个字符串中是否包含另一个字符串,可以使用strstr()函数.使用示例如下: int main(int argc, char *argv[]) {char ...

  5. es6学习笔记-字符串的扩展_v1.0_byKL

    es6学习笔记-字符串的扩展_v1.0 字符的Unicode表示法 JavaScript 允许使用uxxxx的形式表示一个字符,但在 ES6 之前,单个码点仅支持u0000到uFFFF,超出该范围的必 ...

  6. IOS学习笔记07---C语言函数-printf函数

    IOS学习笔记07---C语言函数-printf函数 0 7.C语言5-printf函数 ------------------------- ----------------------------- ...

  7. jquery学习笔记及常用函数封装

    二.JQuery 学习笔记及常用函数封装 https://download.csdn.net/download/weixin_42530002/13087988 1.JQuery入门 (1).css选 ...

  8. lua基础学习笔记-字符串

    lua基础学习笔记-字符串 字符串 Lua 语言中字符串可以使用以下三种方式来表示: 单引号间的一串字符. 双引号间的一串字符. [[ 与 ]] 间的一串字符. 例: string1 = " ...

  9. Hive学习笔记三之函数操作

    文章目录 5 函数 5.1 系统内置函数 5.2 常用内置函数 5.2.1 空字段赋值 5.2.2 CASE WHEN THEN ELSE END(类似于java中的switch case) 5.2. ...

最新文章

  1. asp在线压缩和解压缩文件(文件夹)
  2. 为什么模型复杂度增加时,模型预测的方差会增大,偏差会减小?
  3. iOS如何转换十三位的时间戳
  4. nVIDIA显卡命名规律
  5. GCC-__attribute__()(一)属性机制
  6. 推荐一个简洁优雅的博客系统,farbox
  7. bzoj 1085: [SCOI2005]骑士精神(IDA*)
  8. Oracle 新建用户、赋予权限
  9. PC端微信扫码关注公众号并登录
  10. Outlook-VBA-05-自动获取邮件附件
  11. 基于遗传算法的神经网络,遗传算法训练神经网络
  12. 迈克尔.杰克逊时代的意义
  13. k8s的service端口暴露与代理
  14. Python实现图片黑白化
  15. MOOS-ivp 实验五 MOOS编程进阶(2)
  16. Mysql查询各门课程成绩大于85分的学生名单—纠正网上大部分文章的错误
  17. 对专业学习的期望与目标
  18. 蒲月“登高”,临风眺望,旷视邀您共赴AI的下一个十年之约
  19. Office EXCEL 2010如何取消宏密码保护
  20. eclipse报错排解

热门文章

  1. 不要再把 pp 写成 % 了。
  2. Luban—— Android图片压缩工具
  3. python中画圆的代码_python实现画圆功能
  4. CSS定位(Positioning)多头借贷查询系统开发网贷信息查询,多头借贷和多头借贷记录,有多重要?
  5. CSS或JS实现逐帧动画方案
  6. 操作系统:进程和线程
  7. pymongo.errors.ConfigurationError: incompatible_err
  8. Node.js开发入门—Express安装与使用
  9. 为什么HTML中输入中文可自动换行而英文不行?
  10. 企业微信hook接口,网络获取外部联系人