函数名: 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;
}/*下面再给你其他相关的函数应用*/函数名: 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);
else
printf("The character was not foundn");
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;
}PS:isalpha()是字符函数,不是字符串函数,
isalpha
原型:extern int isalpha(int c);
用法:#include <ctype.h>
功能:判断字符c是否为英文字母
说明:当c为英文字母a-z或A-Z时,返回非零值,否则返回零。
举例:
// isalpha.c
#include <syslib.h>
#include <ctype.h>
#include <stdio.h> main()
{
int c;
clrscr(); // clear screen
printf("Press a key");
for(;;)
{
c=getchar();
clrscr();
printf("%c: %s letter",c,isalpha(c)?"is":"not");
}
return 0; // just to avoid warnings by compiler
}
19
| 评论

C++strcmp用法相关推荐

  1. c语言字符串函数strcat strcpy strlen strcmp的用法及原型

    目录 strcat的用法及原型 strcpy的用法及原型 strcmp用法及原型 strlen用法及原型 strcat的用法及原型 strcat(str1,str2) 意为将字符串str2连接到字符串 ...

  2. C语言字符串函数strcat | strcpy | strlen | strcmp的用法及原型

    点击蓝字 关注我们 因公众号更改推送规则,请点"在看"并加"星标"第一时间获取精彩技术分享 来源于网络,侵删 strcat(str1,str2) 意为将字符串s ...

  3. C语言:strcmp()---字符串比较

    C语言 基础开发----目录 一.strcmp()简介 1. 函数原型 int strcmp(const char *s1,const char *s2); 2. 参数 s1– 指向字符串的指针 s2 ...

  4. C语言实现模拟用户登录

    目录 一.问题要求 二.解决思路 库函数strcmp() 用法: 例子: 一.问题要求 模拟用户登录情况,用户输入密码,如果密码正确就提示登陆成功,错误就显示登录失败,且只能有三次机会,输入错误三次就 ...

  5. 输入一个字符串,判断其是否是回文。(回文:即正读和反读都一样,如abccba, abccba)

    输入一个字符串,判断其是否是回文.(回文:即正读和反读都一样,如abccba, abccba) 这里讨了个巧用了strcmp函数 注:strcmp用法: 字符串比较函数,一般形式为strcmp(字符串 ...

  6. C++20尝鲜:<=>三路比较运算符(Three-way comparison)

    三路比较运算符 <=> 也被称为宇宙飞船运算符(spaceship operator). 三路比较运算符表达式的形式为: 左操作数<=>右操作数 为什么引入 如果项目中使用st ...

  7. 听说分享笔记会奖励粮票的,我也发一篇

    由于咱们兄弟连限制下载,所以内容直接写下去了,复制粘贴吧 所有内容都是写在PHP文件的,新建一个PHP复制到里面吧,有充分的注释,都是自己写的例子,名称都是$a $b之类的,凑合看吧 /*------ ...

  8. php自然排序法的比较过程,PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)...

    本文实例讲述了PHP中strnatcmp()函数"自然排序算法"进行字符串比较用法.分享给大家供大家参考,具体如下: PHP中strnatcmp()函数使用"自然&quo ...

  9. php strcmp函数用法,php字符串比较函数用法小结(strcmp,strcasecmp,strnatcmp及strnatcasecmp)...

    本文实例分析了php字符串比较函数用法.分享给大家供大家参考,具体如下: 直接比较字符串是否完全一致,可以使用"=="来进行,但是有时候可能需要进行更加复杂的字符串比较,如部分匹配 ...

最新文章

  1. gerrit上sshkey设置问题
  2. ERP选型技巧之“三不要一要”
  3. 关于接口 RandomAccess
  4. 云路由 vyatta 体验(一)基本设置
  5. 移动端导出excel_连载系列【4】Excel开发移动端quot;APPquot;
  6. 搭建git服务器--ssh篇
  7. 用matlab的ADC和DAC过程,谈谈我理解的ADC和DAC
  8. 4.4 Triplet 损失
  9. Factory method ‘redisConnectionFactory‘ threw exception; nested exception is java.lang.NoClassDefFou
  10. beamer插入图片_利用Pandoc将Markdown转化为beamer(七)Pandoc的命令行参数
  11. VS2022怎么取消背景或者删除主题
  12. html5电子时表,HTML5 canvas钟表
  13. Unknown column 'xxx' in 'field list'
  14. Alibaba Cloud 3 (Soaring Falcon) x86_64(Py3.7.8) 系统 YUM源
  15. 使用python对股票数据分析预测
  16. FormulaR1C1是公式输入方法
  17. GAMES101 梳理 / 个人向图形学笔记
  18. c#用串口传输二进制文件 xmodem协议
  19. 获取bing壁纸php,php获取bing每日壁纸的示例
  20. 程序员 面试如何介绍自己

热门文章

  1. SIR SIRE 传染病预测模型与代码应用之概念篇
  2. 解决无公网IP,远程访问黑群晖NAS
  3. mysql安装2503,无法安装msi格式软件提示错误代码2502、2503怎么办?
  4. Centos8.5无法用yum安装screen,iftop,nethogs等的解决办法
  5. 用ThoughtWorks.QRCode生成二维码时出现“索引超出了数组界限”的错误
  6. 15.正则表达式以及Data以及Calendar
  7. matlab函数之bsxfun
  8. 消息队列KafKa的集群部署
  9. 宋红康老师JVM课程学习笔记
  10. unicode和字符串之间的转换有两种方式