zoukankan      html  css  js  c++  java
  • 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] = {''};
            char strOut[10000] = {''};
            int count['Z'] = {''};
    
            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
    ", 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] = {''};
    
            gets(strIn); //从键盘读取字符串
            size = (int)strlen(strIn); 
            for(int j = 0; j < size;)
            {
                int k = 0;
                if(strIn[j] >= 'A' && strIn[j] <= 'Z')
                {
                    char temp = '';
                    temp = strIn[j];
                    j++;
                    k = 1;
                    while(strIn[j] == temp) //判断接下来是否有相同的字母
                    {    
                        j++;
                        k++; //统计相同的字母个数
                    }
                    //输出个数和字母
                    if(k > 1)
                        printf("%d", k);
                    printf("%c",temp);
                }
            }
            printf("
    ");
        }
    }
    
    
    
     
  • 相关阅读:
    敏捷开发第五天
    敏捷开发第四天
    系统用户分析模型
    第三天敏捷开发
    第二天敏捷开发
    敏捷开发第一天
    第三周学习总结
    [学习笔记]莫队算法
    【网络流】Modular Production Line
    [学习笔记]set的使用
  • 原文地址:https://www.cnblogs.com/mrethan/p/4636520.html
Copyright © 2011-2022 走看看