//转化字符串为原来字符+该字符连续出现的次数 void TransChar(char* str,int n,char* res) { int res_len=0;//结果字符串现有长度 int count =1;//当前字符出现的次数 for (int i=0;i<n;i++) { if (str[i+1]==str[i]) { count++; } else { sprintf(res+res_len,"%c%d",str[i],count); res_len+=2; count =1; } } } //替换字符串里面的空格为%20,保存在res void repalcebank(char* str,int n,char* res) { int res_len=0;//记录存储在res中字符长度 for (int i=0;i<=n;i++) { if (str[i]==' ') { char sub[]="%20"; sprintf(res+res_len,"%s",sub);//把%20加在res字符串尾端 res_len+=3; } else { res[res_len++]=str[i]; } } res[res_len]='\0';//添加结束符 } int main() { char a[]="we are happy"; char res[20]={'\0'}; repalcebank(a,12,res); return 0; }
时间: 2024-12-28 16:43:23