其他分享
首页 > 其他分享> > 字符串2

字符串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);

 

标签:const,int,32,char,sprintf,str,字符串
来源: https://www.cnblogs.com/yaotong0830/p/16029227.html