string的常用操作与函数
1.可以用保存在字符数组中的字符串直接进行赋值
#include <iostream>
#include <string>
using namespace std;
int main()
{
char str[]="hello world";
string s = str;
cout<<s<<endl;
}
2.删除其中的字符 //下例从下标6开始删除3个字符
3.寻找子串,若找到返回第一次找到的下标,否则返回一个string::npos
4.插入字符串 //下例在第一个参数为下标前插入子串
104题解
题目:
给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串。
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
char str[101];
gets(str);
string target=str;
for (int j=0; j<target.size(); j++) {
target[j]=tolower(target[j]);
}
while (gets(str)) {
string s=str;
string c=s;
for (int i=0; i<c.size(); i++) {
c[i]=tolower(c[i]);
}
int startpos = 0;
string::size_type value = c.find(target,startpos);
while (value!=string::npos) {
s.erase(value,target.size());
c.erase(value,target.size());
value = c.find(target,value);
}
value = s.find(' ',startpos);
while (value!=string::npos) {
s.erase(value,1);
value = s.find(' ',value);
}
cout<<s<<endl;
}
}