C++字符串

· · 个人记录

首先声明本博客中用于测试的字符串为:

Cgp only eight buckets, learned rich five cars alone, great righteousness, help people.

中文的翻译是:

cgp才高八斗、学富五车 , 独当一面, 助人为乐。

直接是不要脸太真实了!

字符串必备头文件:

include<string>

include<cstring>

include<cstdio>

1.char数组

(1).输入输出方法:

<1>cin-cout:

只能读入单个单词(到空格结束)

#include<bits/stdc++.h>
using namespace std;
char s1[10000]; 
int main()
{
    cout<<"输入:"<<endl;
    cin>>s1;
    cout<<"输出:"<<endl;
    cout<<s1; 
    return 0;
}

<2>gets()-puts()

可以读入句子(到'/n'结束) 读入一个完整的行(从标准输入读,一直读到遇到换行符),把读到的内容存入括号中指定的字符数组里,并用空字符'\0'取代行尾的换行符'\n'。读入时不需要考虑换行符。

#include<bits/stdc++.h>
using namespace std;
char s1[10000]; 
int main()
{
    cout<<"输入:"<<endl;
    gets(s1);
    cout<<"输出:"<<endl;
    puts(s1); 
    return 0;
}

<3>getchar()

只能读入单个字符(字母)

#include<bits/stdc++.h>
using namespace std;
char a[10000]; 
int n;
int main()
{
    cout<<"输入:"<<endl;
    while((a[n]=getchar())!='\n')
    {
        n++;
    }
    cout<<"输出:"<<endl;
    for(int i=0;i<n;++i)
    {
        cout<<a[i];
    }
    return 0;
}

<4>getline()

读入不需要考虑最后的换行符,但是需要知道长度

char s12[1024];
cin.getline(s1,1024);

<5>scanf

scanf(“%s”,字符串名称);

字符串名称之前不加&这个取地址符。例如:scanf(“%s”,&s1)是错误的,&表示取地址,而字符串的名即为地址s1[0]

注意:

Scanf读入char[]:读字符串时忽略开始的空格,并且读到空格为止,因此只能读取一个单词,而不是整行字符串.

#include<bits/stdc++.h>
using namespace std;
char s1[100];
int main()
{   
    cout<<"输入:"<<endl;
    scanf("%s",s1);
    cout<<"输出:"<<endl;
    printf("%s",s1);
    return 0;
}

常用函数:

1.strlen(字符串名)

计算字符串的长度,终止符’\0’不算在长度之内,可以放心食用

len=strlen(a);
for(int i=0;i<len;++i)
{
    //.....
}

取字符串长度要在for外面定义,否则会超时(已经被坑过了) 还有一点字符串从0开始

2.strcmp(字符串名1,字符串名2)

strcmp(字符串名1,字符串名2)

比较字符串1和字符串2的大小,比较的结果由函数带回;

如果字符串1>字符串2,返回一个正整数;

如果字符串1=字符串2,返回0;

如果字符串1<字符串2,返回一个负整数;

比较方式为诸位比较

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
char a[1000],b[1000];
int jsa[1000],lena,lenb,jsb[1000];
int main() {
    char x;
    while((x=getchar())!='\n') {
        if(x!=' ') {
            a[lena]=x;
            lena++;
        }
    }
    while((x=getchar())!='\n') {
        if(x!=' ') {
            b[lenb]=x;
            lenb++;
        }
    }
    for(int i=0; i<lena; ++i) {
        if(a[i]>='A'&&a[i]<='Z') {
            a[i]+=32;
        }
    }
    for(int i=0; i<lenb; ++i)
        if(b[i]>='A'&&b[i]<='Z') {
            b[i]+=32;
        }
    if(!strcmp(a,b))
    {
        cout<<"YES";
    }
    else 
        cout<<"NO";
    return 0;
}

代码来源 openjudge 1.7 17

3.strlwr(字符串名)

将字符串中大写字母换成小写字母

也可以用a[i]+32实现

4.strupr(字符串名)

将字符串中小写字母换成大写字

也可以用a[i]-32实现

5.strcpy(字符串名1,字符串名2)

将字符串2复制到字符串1,返回字符串1的

#include<bits/stdc++.h>
using namespace std;
char a[100];
char b[100];
int main()
{
    gets(a);
    strcpy(b,a);
    puts(b);
    return 0;
}

string字符串

读入输出方式:

1.cin-cout

读到空格结束(只读一个单词)

#include<bits/stdc++.h>
using namespace std;
string s1; 
int main()
{
    cout<<"输入:"<<endl;
    cin>>s1;
    cout<<"输出:"<<endl;
    cout<<s1; 
    return 0;
}

2.getline(cin,s1)

可以读取句子,注意没有putline(cout,s1);

#include<bits/stdc++.h>
using namespace std;
string s1; 
int main()
{
    cout<<"输入:"<<endl;
    getline(cin,s1);
    cout<<"输出:"<<endl;
    cout<<s1;
    return 0;
}

2.骚操作:

1.定义

string s1();  // si = ""
string s2("Hello");  // s2 = "Hello"
string s3(4, 'K');  // s3 = "KKKK"
string s4("12345", 1, 3);  //s4 = "234",即 "12345" 的从下标 1 开始,长度为 3 的子串

2.赋值

string 类还有 assign 成员函数,可以用来对 string 对象赋值。assign 成员函数返回对象自身的引用。例如:
纯文本复制
string s1("12345"), s2;
s3.assign(s1);  // s3 = s1
s2.assign(s1, 1, 2);  // s2 = "23",即 s1 的子串(1, 2)
s2.assign(4, 'K');  // s2 = "KKKK"
s2.assign("abcde", 2, 3);  // s2 = "cde",即 "abcde" 的子串(2, 3)

3.连接

string s1("123"), s2("abc");
s1.append(s2);  // s1 = "123abc"
s1.append(s2, 1, 2);  // s1 = "123abcbc"
s1.append(3, 'K');  // s1 = "123abcbcKKK"
s1.append("ABCDE", 2, 3);  // s1 = "123abcbcKKKCDE",添加 "ABCDE" 的子串(2, 3)

也可以是 s3=s1+s2

4.没想好题目

1.s1+s2可以把s1,s2连接在一起;

2.s1=s2把s2赋值给s1;

3.s1==s2可以判断s1,s2是否相等

常见函数:

1.str1.assign("ABC");     //清空string串,然后设置string串为"ABC"
2.str1.length();或者s1.size()  //获取字符串长度

3.str1.swap(str2);              //替换str1 和 str2 的字符串

4.str1.clear();        //删除所有

5.str1.empty();            //判断为空, 为空返回true

6.str1.replace(2,4, "ABCD"); //从下标为2的位置,替换4个字节,为"ABCD"

7.字符串名.find()用法:(贼好用了!!)

string str("ABCDEFGABCD"); //11 个字符

int n;<br>

n= str.find('A'); //查找"A",n=0;

n= str.find("AB"); //查找"AB",n=0;

n= str.find("BC",1); //从位置 1 处,查找"BC",n=1;

n= str.find("CDEfg",1,3); //从位置 1 处,查找"CDEfg"的前 3 个字符,
等价于 str.find("CDE",1),n=2;

/*rfind():反向查找,从末尾处开始,向前查找*/

n= str.rfind("CD"); //从位置 10 开始向前查找,n=9

n= str.rfind("CD",5); //从位置 5 开始向前查找,n=2

n= str.rfind("CDEfg",5,3); //等价于 str.rfind("CDE",5);

题目 openjudge 1.8 26 字符串最大跨距

代码:

#include<string>
#include<algorithm>
#include<iomanip>
#include<iostream>
#include<cmath>
using namespace std;
int i,j,n,l,a[1000],x,y,z;
string s,s1,s2 ;
int main() {
    getline(cin,s,',');
    getline(cin,s1,',');
    getline(cin,s2);
    int l1,l2;
    l1=s.find(s1);
    l2=s.rfind(s2);
    int l=s1.length();
    if(l2<l1||l2==-1||l1==-1) { 
        cout<<-1;
    } else { 
        cout<<l2-l1-l;
    }
    return 0;
}

如果xx.find()没找到将返回-1

真正骚气牛逼的来了:

将string类型直接转换成int 整形的函数(不需要把字符转换成数字)

int numbers=atoi(shu.c_str());//shu为字符串名

需要在#include<algorithm>中

其实还有一种方法啦:

将string 转成int
方法二:

sscanf(st.c_str(),"%d",&i);

如果你要问sscanf是什么 请点这里 代码:

题目:求字符串里的数字和

例子:

Cgp 250 eight buckets, learned rich 5 cars alone, great righteousness, help people.

输出:255

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
string a,shu;
long long int he,len;
int i,j;
int main()
{
    int begin,end;
    getline(cin,a);
    len=a.length();
    for( i=0;i<len;++i)
    {
        if(a[i]>='0'&&a[i]<='9')
        {
            begin=i;
            end=i;
            for(j=begin;;++j)
            {
                if(a[j]<'0'||a[j]>'9')
                    break;
                end++;  
            }
            int p=0;
            for(int k=begin;k<=end;++k)
            {
                shu[p]=a[k];
                p++;
            } 
            int numbers=atoi(shu.c_str());
            he+=numbers;
            i=end;
        }
    }
    cout<<he;
    return 0;
}

题目坑爹的地方:

1.当输入时1个整数(或浮点)加回车然后读入两行(以上)的字符串时:

如果你是用

cin>>n;
gets(a);
gets(b);

恭喜你你死在输入上了,当你输入完n按了回车后,gets(a)已经结束了(O(∩_∩)O哈哈哈~)

#include<bits/stdc++.h>
using namespace std;
string a,b;
int len,js;
double x,xx;
int main()
{
    cin>>x>>a>>b;
    len=a.length();
    for(int i=0;i<len;++i)
    {
        if(a[i]==b[i])      
    js++;
    }
    xx=js*1.0/len;
    if(xx>=x)
    {
        cout<<"yes";
    }
    else 
        cout<<"no";
    return 0;
}

2.坑爹的输入方式:

比如输入的字符串以逗号分隔(你妹的出题人光想坑我们

下面介绍一下怎么输入:

    getline(cin,s,',');//输入逗号以前的字符
    cin.clear();//清空输入
    getline(cin,s1,',');
    cin.clear();
    getline(cin,s2);

哇!cgp太牛逼不要脸了!

输入输出总结:

整句读入(带空格或不带都可)

可以用gets(),getline(cin,s)

去除某些元素(如空格等)

可以用while()+grtchar读入;

或者while(cin>>x)+if()+赋值给字符数组(注意x应为char)

getline(cin,s,'要结束的符号')

打字不容易please看完评论+点赞

本博客未完待续,等做多点题后补充,欢迎再评论区指正错误或添加知识点