字符数组*char
有关的应用函数:
1.字符串复制函数strcpy
其函数原型为strcpy(char[],const char[]);
strcpy是string copy(字符串复制)的缩写。它的作用是将第二个字符数组中的字符串复制到第一个字符数组中去,将第一个字符数组中的相应字符覆盖。
eg.
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char str1[10];
char str2[]={
"China"};
strcpy(str1,str2);
cout << str1 << endl;
return 0;
}
2.字符串长度函数strlen
strlen是string length(字符串长度)的缩写。
它是测试字符串长度的函数。其函数的值为字符串中的实际长度,不包括′\0′在内。eg.
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char str1[]={
"China"};
int len=strlen(str1);
cout << len << endl;
return 0;
}
3…字符串比较函数strcmp (可用于判断两个字符串是否相等)
strcmp(const char[],const char[]);
strcmp是string compare(字符串比较)的缩写。作用是比较两个字符串。
如果字符串1=字符串2,函数值为0。
如果字符串1>字符串2,函数值为一正整数。
如果字符串1<字符串2,函数值为一负整数。
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char str1[]={
"English"};
char str2[]={
"China"};
int judge=strcmp(str1,str2);
cout << judge << endl;
return 0;
}
4…字符串连接函数 strcat
其函数原型为strcat(char[],const char[]);
该函数有两个字符数组的参数,函数的作用是:将第二个字符数组中的字符串连接到前面字符数组的字符串的后面。第二个字符数组被指定为const,以保证该数组中的内容不会在函数调用期间修改。连接后的字符串放在第一个字符数组中,函数调用后得到的函数值,就是第一个字符数组的地址。
#include<cstdio>