1.
#include <iostream>
#include <cstring>
#include <algorithm>
#include<string>
#include<stack>
using namespace std;
void trans(string &s1,char a,char b)
{
for(int i=0;i<s1.size();i++)
{
if(s1[i]==a)
{
s1[i]=b;
}
}
}
int main() {
string s1;
char a,b;
cin>>s1>>a>>b;
trans(s1,a,b);
cout<<s1<<endl;
}
2.
#include <iostream>
#include <cstring>
#include <algorithm>
#include<string>
#include<stack>
using namespace std;
void trans(string &s1)
{
for(int i=0,j=s1.size()-1;i<j;i++,j--)
{
swap(s1[i],s1[j]);
}
}
int main() {
string s1;
cin>>s1;
trans(s1);
cout<<s1<<endl;
}
3
#include <iostream>
#include <string>
using namespace std;
void delete_string(string &s, char c) {
int n = s.size();
int i = 0;
while (i < n) {
if (s[i] == c) {
// 将当前字符及其后面的字符前移
for (int j = i; j < n - 1; j++) {
s[j] = s[j + 1];
}
n--;
} else {
i++;
}
}
s.resize(n);
}
int main() {
string s;
char c;
cin >> s >> c;
cout<<s.size()<<endl;
delete_string(s, c);
cout << s << endl;
cout<<s.size()<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
void delete_string(string &s, char c) {
int n = s.size();
int i = 0;
int index=0;
while (i < n) {
if(s[i]!=c)
{
s[index++]=s[i];
}
i++;
}
s.resize(index);
}
int main() {
string s;
char c;
cin >> s >> c;
delete_string(s, c);
cout << s << endl;
return 0;
}
4
#include <iostream>
#include <string>
using namespace std;
void delete_string(string &s, int a,int b) {
int n=s.size();
int index=0;
if(a+b>n)b=n-a;
for(int i=0;i<n;i++)
{
if(i<a||i>=a+b)
{
s[index++]=s[i];
}
}
s.resize(index);
}
int main() {
string s;
int a,b;
getline(cin,s);
cin>>a>>b;
delete_string(s,a,b);
cout<<s<<endl;
}
3
#include <iostream>
#include <string>
using namespace std;
void replace_substring(string &s1, const string &s2, int i, int j) {
// 确保 i 和 j 在有效范围内
if (i < 0 || i >= s1.size() || j < i || j >= s1.size()) {
cout << "Invalid indices" << endl;
return;
}
// 生成替换后的字符串
s1 = s1.substr(0, i) + s2 + s1.substr(j + 1);
}
int main() {
string s1, s2;
int i, j;
// 读取输入
cin >> s1 >> s2 >> i >> j;
// 调用函数进行替换
replace_substring(s1, s2, i, j);
// 输出结果
cout << s1 << endl;
return 0;
}
4
#include <iostream>
#include <string>
using namespace std;
int count_times(string s,string s1)
{
int i=0;//主串指针
int j=0;//模式串指针
int count=0;//记录匹配次数
int n=s.size();
int n1=s1.size();
while(i<n&&j<n1)
{
if(s[i]==s1[j])
{
i++;
j++;
}
else
{
j=0;
i++;
}
if(j==n1-1)
{
count++;
j=0;
}
}
return count;
}
int main() {
string s, s1;
cin>>s>>s1;
cout<<count_times(s,s1)<<endl;
}
5
#include <iostream>
#include <cstring>
using namespace std;
struct Node {
char val;
Node *next;
Node(int x) : val(x), next(NULL) {}
};
Node *CreateListWithTail(char s[], int n) {
Node*dummy=new Node(-1);
Node*tail=dummy;
for(int i=0;i<n;i++)
{
Node*node=new Node(s[i]);
tail->next=node;
tail=node;
}
return dummy->next;
}
void print(Node *head)
{
for (Node *p = head; p; p = p->next)
cout << p->val << ' ';
cout << endl;
}
char first_unfind(Node*list1,Node*list2)
{
while (list1) {
Node* cur2 = list2;
bool is_find = false;
while (cur2) {
if (cur2->val == list1->val) {
is_find = true;
break;
}
cur2 = cur2->next;
}
if (!is_find) {
return list1->val;
}
list1 = list1->next;
}
return '\0'; // 如果所有字符都在list2中找到,返回 '\0'
}
int main() {
char s1[]="abcde";
char s2[]="abccc";
int n1 =strlen(s1);
int n2 =strlen(s2);
Node *list1 =CreateListWithTail(s1, n1);
Node *list2= CreateListWithTail(s2, n2);
cout<<first_unfind(list1,list2)<<endl;
return 0;
}