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("
    ");
        }
    }
    
    
    
     
  • 相关阅读:
    51 张图助你彻底掌握 HTTP
    Nginx从原理到实战
    vu3.0 + ts + swiper6 的问题
    使用 react-router-dom v5 查询query 参数的方法
    visual studio 2015配置SVN
    SVN使用教程总结
    C#与SAP进行数据交互
    shell csv/txt文件对比
    persto array_join(array_agg(),',')
    shell 拼接html table 发送邮件
  • 原文地址:https://www.cnblogs.com/mrethan/p/4636520.html
Copyright © 2011-2022 走看看