字符数组*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>
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char str1[]={"I love "};
char str2[]={"China!"};
strcat(str1,str2);
cout << str1 << endl;
return 0;
}
5.字符查找函数strstr
其函数原型为strcat(const char[],const char[]);
寻找某字符串在另一字符串中第一次出现的位置,并返回查找到字符串的位置之后的全部字符串。
(1)参数一是字符串类型的参数,要传入的是被查找的字符串。
(2)参数二是字符串类型的参数,传入的是要查找的字符串。
(3)strstr函数会寻找参数二在参数中出现的位置,并返回查找到字符串的位置之后的全部字符串。当没有查找到符合的字符串时,strstr函数会返回 FALSE(布尔值)。
#include
using namespace std;
int main(){
char a[5],b[20];
char *str=strstr(a,b);
printf("%s\n",str);
return 0;
}
用于寻找首次出现位置
#include
using namespace std;
int main(){
char a[5],b[20];
char *str=strstr(a,b);
printf("%d\n",str-b);
return 0;
}
6.判断字母是否是大写isupper与字母的大小写转化tolower(把大写转化为小写)与toupper(把小写转化为大写)
#include<bits/stdc++.h>
using namespace std;
void strlower(char *a){//变小写的函数
for(int i=0;i<strlen(a);i++)
if(isupper(a[i])){ // isupper是判断这个字符是不是大写字母
a[i]=tolower(a[i]);
// tolower会把这个字母从大写转为小写
}
}
int main (){
char a[100];
gets(a);
strlower(a);
cout << a << endl;
return 0;
}
string类
区别:
1.string与char的区别string是一个类型,一个变量就可以代表字符串,char a[ ] 只是一个字符数组。
2.和其他类型变量一样,字符串变量必须先定义后使用,定义字符串变量要用类名string。如:
string string1; //定义string1为字符串变量string
string2=″China″; //定义string2同时对其初始化
应当注意: 要使用string类的功能时,必须在本文件的开头将C++标准库中的string头文件包含进来,即应加上
#include //注意头文件名不是string.h
1.连接两个字符串(不需要函数,直接加起来)
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
int main(){
string s1={"I love "};
string s2={"China!"};
s1=s1+s2;
cout << s1 << endl;
return 0;
}
2.得到字符串长度(包括空格,末尾不包括 ‘\0’ )
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
int main(){
string s1={"I love "};
string s2={"China!"};
s1=s1+s2;
int len=s1.length();
cout << len << endl;
return 0;
}
3.字符串比较(与char类似的返回值)
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
int main(){
string s1={"I love "};
string s2={"China!"};
if(s1==s2){
cout << "YES" << endl;
}
return 0;
}
4.交换两个字符串swap
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
int main(){
string s1={"I love "};
string s2={"China!"};
swap(s1,s2);
cout << s1 << endl;
return 0;
}
5.字符串的插入insert
#include<bits/stdc++.h>
using namespace std;
int main(){
string str1("I love ");
string str2("China!");
string a,b;
a=str1.insert(7,str2);
cout << a << endl;
str1="I love ";
b=str1.insert(7,3,'x');//在7也就是(0+6)位置插入3三个 x
cout << b << endl;
return 0;
}
6.判断字符串是否为空empty
如果字符串为空,则返回1
若字符串不为空,则返回0
#include<bits/stdc++.h>
using namespace std;
int main(){
string s1("\0");
int a=s1.empty();
cout << a << endl;
return 0;
}
7.字符的删除erase
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除 position 处的一个字符
(3)erase(first,last);删除从 first 到 las t之间的字符
#include<bits/stdc++.h>
using namespace std;
int main(){
string s1("I love China!");
s1.erase(2,4);
cout << s1 << endl;
//第一种用法(删除2位置后面的4个字符)
s1="I love China!";
s1.erase(s1.begin()+5);
cout << s1 << endl;
//第二种,删除特定位置的字符
s1="I love China!";
s1.erase(s1.begin()+1,s1.end()-6);
cout << s1 << endl;
//第三种
return 0;
}
字符串数组
不仅可以用string定义字符串变量,也可以用string定义字符串数组。
如:
string name[5]; //定义一个字符串数组,它包含5个字符串元素
string name[5]={″Zhang″,″Li″,″Fun″,″Wang″,″Tan″};//定义一个字符串数组并初始化
关于字符串数组的几点说明:
1.在一个字符串数组中包含若干个(现为5个)元素,每个元素相当于一个字符串变量。
2.并不要求每个字符串元素具有相同的长度,即使对同一个元素而言,它的长度也是可以变化的,当向某一个元素重新赋值,其长度就可能发生变化。
3.在字符串数组的每一个元素中存放一个字符串,而不是一个字符,这是字符串数组与字符数组的区别。如果用字符数组存放字符串,一个元素只能存放一个字符,用一个一维字符数组存放一个字符串。
4.每一个字符串元素中只包含字符串本身的字符而不包括′\0′。
#include<bits/stdc++.h>
using namespace std;
int main (){
string s[5];
for(int i=0;i<5;i++){
cin >> s[i];
}
for(int i=0;i<5;i++){
cout << s[i] << ' ';
}
cout << endl;
return 0;
}