1The input format of the conversion string is: the character in the original string + the number of consecutive occurrences of the character, such as string:1233422222, Convert1121324125.
we use sprintf , similar to printf .
# Include "iostream" # include "string" using namespace STD; int main () {cout <"Enter the numbers:" <Endl; string STR; CIN> STR; size_t ilength = Str. length (); int icount = 1; char cmychar [50]; cmychar [0] = '\ 0'; int K; For (k = 0; k <ilength-1; k ++) {If (STR [k] = STR [k + 1]) // pre-judgment {icount ++; // number of record characters} else {sprintf (cmychar + strlen (cmychar), "% C % d", STR [K], icount); icount = 1 ;}} if (STR [k] = STR [k-1]); elseicount = 1; sprintf (cmychar + strlen (cmychar), "% C % d ", STR [K], icount); cout <cmychar <Endl; return 1 ;}
2. Convert integers into strings
# Include "iostream" using namespace STD; int main () {int imydata; CIN> imydata; char ctotransfer [56]; char ctemp [56]; int icount = 0; // Number of BITs marked while (imydata) {ctemp [icount ++] = imydata % 10 + '0'; imydata = imydata/10 ;} ctemp [icount] = '\ 0'; cout <ctemp <Endl; icount --; Int J = icount, I = 0; while (j> = 0) {ctotransfer [I ++] = ctemp [j --];} ctotransfer [I] = 0; cout <ctotransfer <Endl; return 1 ;}
3. convert a string to an integer
# Include "iostream" using namespace STD; int main () {char imydata [56]; CIN> imydata; int itotransfer [56]; int ctemp [56]; int icount = 0; // indicates the number of BITs int isum = 0; while (imydata [icount]) {isum = isum * 10 + imydata [icount]-'0 '; icount ++;} cout <isum <Endl; return 1 ;}