HDU1020字符串操作
#include <stdio.h> #include <string.h> int N; int size; void main() { scanf("%d", &N); getchar(); //读取残留的回车符 for(int i = 0; i < N; i++) { int index = 0; char strIn[10000] = {'\0'}; char strOut[10000] = {'\0'}; int count['Z'] = {'\0'}; gets(strIn); //从键盘读取字符串 size = (int)strlen(strIn); for(int j = 0; j < size; j++) { if(strIn[j] >= 'A' && strIn[j] <= 'Z') { count[strIn[j]]++; //统计字母个数 } else return; } for(int i = 'A'; i < 'Z'; i++) //输出每个字母的个数 { if(count[i] != 0) //如果输入的字符串包含此字符 { if(count[i] == 1) { strOut[index++] = i; } else { int len = 0; char str[5] = {'/0'}; //strOut[index++] = count[i] + '0'; //统计相同字符个数 len = sprintf(str, "%d", count[i]); strcat(&strOut[index], str); index += len; strOut[index++] = i; } } } printf("%s\n", strOut); } }
#include <stdio.h> #include <string.h> int N; int size; void main() { scanf("%d", &N); getchar(); //读取残留的回车符 for(int i = 0; i < N; i++) { char strIn[10000] = {'\0'}; gets(strIn); //从键盘读取字符串 size = (int)strlen(strIn); for(int j = 0; j < size;) { int k = 0; if(strIn[j] >= 'A' && strIn[j] <= 'Z') { char temp = '\0'; temp = strIn[j]; j++; k = 1; while(strIn[j] == temp) //判断接下来是否有相同的字母 { j++; k++; //统计相同的字母个数 } //输出个数和字母 if(k > 1) printf("%d", k); printf("%c",temp); } } printf("\n"); } }