C++字符型数组常⽤函数
C++字符型数组常⽤函数
函数名: strcmp
功 能: 串⽐较
⽤ 法: int strcmp(char *s1, char *s2);
当s1
#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");
return0;
}
/下⾯再给你其他相关的函数应⽤/
函数名: 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);
cstring转为int
printf("%sn", string);
return0;
}
函数名: 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);
return0;
}
原型:strlen( const char string[] );
功能:统计字符串string中字符的个数
例程:
#include <iostream>
#include <cstring>
int main()
{
char str[100];
cout <<"请输⼊⼀个字符串:";
cin >>str;
cout <<"The length of the string is :"<<strlen(str)<<"个"<<endl;
return0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。