字符串2

一串数字,每两位一组,不够补0,加上32,放到新的字符串里,比如123456,12+32=44,34+32=66,56+32=88

复制代码
#include <bits/stdc++.h>
using namespace std;

char str[101]={'\0'};
char res[101]={'\0'};
int main(){
    scanf("%s",&str);
    int len=strlen(str);
    if(len%2!=0){
        str[len++]='0';
    }
    int reslen=0;
    for(int i=0;i<len;i+=2){
        char temp[3]={'\0'};
        temp[0]=str[i];
        temp[1]=str[i+1];
        int num=atoi(temp);
        num+=32;
        sprintf(res+reslen,"%d",num);
        char temp1[100]={'\0'};
        itoa(num,temp1,10);
        reslen+=strlen(temp1);
    }
    cout<<res<<endl;
    //test();
    return 0;
}
复制代码

总结一下字符串和整数的相互转化

//char *str
//字符串转整数 int atoi ( const char * str ); double atof ( const char * str ); long int atol ( const char * str ); int sscanf ( const char * str, const char * format, ...); int a;float b;long c; sscanf ("32,3.1415,567283","%d,%f,%d",&a,&b,&c);
//整数转字符串
char *  itoa ( int value, char * str, int base );
itoa(num,temp1,10);
int sprintf ( char * str, const char * format, ... );
char a[10],b[10],c[10];
sprintf(a, "%d", 32);
sprintf(b, "%f", 3.1415);
sprintf(c, "%d", 567283);

 

输入字符串,共三问,去除前面的空格,中间多个空格只保留一个空格,数字字母中间

加上_

 

复制代码
#include <bits/stdc++.h>
using namespace std;

int main(){
    string str,res;
    getline(cin,str);
    bool start=false;
    int i=0,len=0;
    while(!start){//处理开头的空格
        if(str[i]!=' '){
            break;
        }
        i++;
    }
    bool fig=false,letter=false,spa=false;
    while(i<str.size()){
        if(str[i]!=' '){
            spa=false;
            if(str[i]>='1'&&str[i]<='9'){
                if(letter){
                    res+='_';
                    letter=false;
                }
                fig=true;
            }
            else{
                if(fig){
                    res+='_';
                    fig=false;
                }
                letter=true;
            }
            res+=str[i];
        }
        else{
            if(!spa){
                res+=str[i];
                spa=true;
                fig=false;
                letter=false;
            }
        }
        i++;
    }
    cout<<res<<endl;
    return 0;
}
复制代码

 

3.无冗余的输入一个字符串

1)输出该字符串

2)对于不是首次出现的字符,对其进行过滤,例如 abcdacdef,过滤后为  abcdef

3)对于字符 0-9,A-F,a-f,将其对应的 ASCII码的低 4位进行对调,例如将 1011,转

换为 1101,并将对应的 ACSII码对应的字符输出,若为字母,转换为大写。

  

复制代码
#include <bits/stdc++.h>
using namespace std;

bool flag[128]={false};

int main(){
    string str,res;
    cin>>str;
    cout<<str<<endl;
    for(int i=0;i<str.size();i++){
        int idx=str[i];
        //cout<<"idx:"<<idx<<endl;
        if(!flag[idx]){
            flag[idx]=true;
            char c=str[i];
            if((c>='0'&&c<='9')||(c>='a'&&c<='f')||(c>='A'&&c<='F')){
                int temp=idx%8;
                idx/=8;
                idx*=8;//末4位取0
                int bit[4];
                for(int j=0;j<4;j++){
                    bit[j]=temp&1;
                    //cout<<bit[j];
                    temp>>=1;
                }
                //cout<<"ge";
                temp=bit[0];
                bit[0]=bit[3];
                bit[3]=temp;
                temp=bit[1];
                bit[1]=bit[2];
                bit[2]=temp;
                temp=0;
                for(int j=3;j>=0;j--){
                    //cout<<bit[j];
                    temp|=bit[j];
                    if(j!=0)
                        temp<<=1;//注意这里比循环次数要少做一次!
                }
                idx|=temp;
                c=idx;
                if(c>='a'&&c<='f'){
                    c-=32;
                }
            }
            res+=c;
        }
    }
    cout<<res<<endl;
    return 0;
}
复制代码

 

 判断8/10/16进制数的合法输入

复制代码
#include <bits/stdc++.h>
using namespace std;

bool judge(string s){
    bool endstatus=false,end2=false;
    int len=s.size();
    if(s[0]!='0'){//按照十进制处理
        for(int i=0;i<len;i++){
            if(s[i]=='u'||s[i]=='U'){
                if(endstatus||i==0||end2){//如果u出现过或在第一位或者出现在l时候都非法
                    return false;
                }
                endstatus=true;
            }
            else if(s[i]=='l'||s[i]=='L'){
                if(i==0||end2){//l出现在第一位或l出现过非法
                    return false;
                }
                end2=true;
            }
            else if(!(s[i]>='0'&&s[i]<='9')){
                return false;
            }
            else{
                if(endstatus||end2)
                    return false;
            }
        }
        return true;
    }
    if(s[0]=='0'&&(s[1]=='x'||s[1]=='X')){//16进制处理
        if(s[2]=='0')
            return false;
        for(int i=3;i<len;i++){
            if(s[i]=='u'||s[i]=='U'){
                if(endstatus||i==0||end2){//如果u出现过或在第一位或者出现在l时候都非法
                    return false;
                }
                endstatus=true;
            }
            else if(s[i]=='l'||s[i]=='L'){
                if(i==0||end2){//l出现在第一位或l出现过非法
                    return false;
                }
                end2=true;
            }
            else if(!((s[i]>='0'&&s[i]<='9')||(s[i]>='A'&&s[i]<='F')||(s[i]>='a'&&s[i]<='f'))){
                return false;
            }
            else{
                if(endstatus||end2)
                    return false;
            }
        }
        return true;
    }
    else{
        if(s[1]=='0')
            return false;
        for(int i=2;i<len;i++){
            if(s[i]=='u'||s[i]=='U'){
                if(endstatus||i==0||end2){//如果u出现过或在第一位或者出现在l时候都非法
                    return false;
                }
                endstatus=true;
            }
            else if(s[i]=='l'||s[i]=='L'){
                if(i==0||end2){//l出现在第一位或l出现过非法
                    return false;
                }
                end2=true;
            }
            else if(!(s[i]>='0'&&s[i]<='7')){
                return false;
            }
            else{
                if(endstatus||end2)
                    return false;
            }
        }
        return true;
    }
}

int main(){
    string s;
    while(cin>>s){
        if(judge(s))
            cout<<"Yes!"<<endl;
        else
            cout<<"No!"<<endl;
    }
    return 0;
}
复制代码

 

 

 

 

posted @   完全感覚Dreamer  阅读(44)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示