1、strchr
#include <stdio.h>
#include <string.h>
char str1[400], str2[400];
char az[400], ax[400], bz[400], bx[400];
int main(void) {
int i, j;
while(~scanf(" %s%s", str1, str2)) {
int p ;
p = strchr(str1, '.') - str1; //字符p在字符串str1中德位置
//printf("%d\n", p);
//getchar();
//getchar();
if(p >= 0) {
for(i=p+1, j=0; str1[i]; i++)
ax[j++] = str1[i];
ax[j] = '\0';
str1[p] = '\0';
}
strcpy(az, str1);
p = strchr(str2, '.') - str2;
if(p >= 0) {
for(i=p+1, j=0; str2[i]; i++)
bx[j++] = str2[i];
bx[j] = '\0';
str2[p] = '\0';
}
strcpy(bz, str2);
puts(az);
puts(ax);
puts(bz);
puts(bx);
}
return 0;
}
2、strstr()
#include<stdio.h>
#include<string.h>
int main(void) {
char *s1 = "I love you";
char *s2 = "ove";
char *p = strstr(s1, s2); //找到字符串s2在s1中的首地址
printf("%s\n", p);
int loca = p-s1; //字符串s2在s1中的位置
printf("%d\n", loca);
return 0;
}
3、strspn()
#include<stdio.h>
#include<string.h>
int main(void) {
char *s1 = "I love you";
char *s2 = "I love ";
int p = strspn(s1, s2);
printf("%d\n", p);
return 0;
}
4、str.length() str.size()
#include <stdio.h>
#include<iostream>
#include <string>
using namespace std;
int main(void) {
string str1, str2;
while(cin >> str1 >> str2) {
int len = str1.length();
int size = str1.size();
printf("Length: %d %d\n", len, str2.length());
printf("Size: %d %d\n", size, str2.size());
}
return 0;
}
5、str.find() str.rfind
#include <iostream>
#include<string>
using namespace std;
int main() {
string str1, str2;
while(cin >> str1 >> str2){
int where = str1.find(str2);
//找到返回第一次出现的位置,找不到返回-1;
cout << str2 <<" in " << str1 << " position: "
<< where << endl;
int pos1 = 3;
where = str1.find(str2, pos1); //pos1指从str1的pos1位置开始往后找
cout << str2 <<" in " << str1 << " position: "
<< where << endl;
where = str1.rfind(str2); //从后往前搜
cout << str2 <<" in " << str1 << " position: "
<< where << endl;
}
return 0;
}
6、str.substr()
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string str1, str2;
while(cin >> str1 ) {
cout << "str1: " << str1<<endl;
//cout << "str2: " << str2<<endl;
int begin = 1;
int len = 3;
str2 = str1.substr(begin, len); //提取字符,参数begin指开始位置,参数len指取字符串长度;
//begin, len可以省略,默认为begin=0,len=str1.length()
cout << "Zuihou:\n\tstr2: " << str2 << endl << endl;
}
return 0;
}
7、str.insert()
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1, str2;
while(cin >> str1 >> str2) {
string temp = str1;
int pos1, pos2;
pos1 = 1;
str1.insert(pos1, str2); //在str1的pos1位置前插入str2, pos1从零开始
cout << str1 << endl;
str1 = temp;
pos1 = 1;
int len2 = 3;
pos2 = 2;
str1.insert(pos1, str2, pos2, len2); //在str1的pos1位置前插入从str2的pos2位置开始长度为len2; pos1从零开始
cout << str1 << endl;
str1 = temp;
pos1 = 1;
int numchar = 5;
char ch = 'c';
str1.insert(pos1, numchar, ch); //在字符串str1的pos1位置插入numchar个字符ch
cout << str1 << endl << endl;
}
return 0;
}
8、str.replace()
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1, str2;
while(cin >> str1 >> str2) {
string temp = str1;
int pos = 1, len=3;
str1.replace(pos, len,str2); //将str1中从pos位置开始长度为len的字符串替换为str2
cout << str1 << endl;
//cout << results << endl;
str1 = temp;
pos = 1;
len = 4;
int numchar=5;
char ch = '8';
//替换字符,从str1的pos开始长度为len的字符串替换为numchar个ch字符
string result = str1.replace(pos, len, numchar, ch);
cout << str1 << endl;
cout << result << endl;
}
return 0;
}
9、swap()
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1, str2;
while(cin >> str1 >> str2) {
cout << "str1: ";
cout << str1 << '\t' << str2 << endl;
swap(str1, str2);
cout << "swap: ";
cout << str1 << '\t' << str2 << endl;
}
return 0;
}
10、str.erase() str.clear()
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
while(cin >> str) {
int pos = 1;//位置
int len = 2;//长度
str.erase(pos, len);
cout << str << endl;
str.clear();
cout << str << endl;
}
return 0;
}
11、str.c_sr()
#include <string>
#include <iostream>
using namespace std;
int main(void) {
string str;
while(cin >> str) {
cout << str << endl;
printf("%s\n", str.c_str());
}
return 0;
}
12、str1+str2 str1.append()
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
string str1, str2, str3;
while(cin >> str1 >> str2) {
str3 = str1 + str2;//字符串附加
cout << "Cout: " << endl;
cout << str3 << endl;
//printf("%s\n", str3.c_str());
//str1.append(str2); //字符串附加
//cout << str1 << endl;
int pos2 = 1, len2 = 2;
str1.append(str2,pos2, len2);//从pos2位置开始, 取长度为2的字符串附加给str1
cout << str1 << endl;
//str3[str3.length()] = '\0';
//printf("%s\n", str3);
}
return 0;
}