【问题标题】:C - Replace from the most to the least common letters in string1 from string2C - 从 string2 替换 string1 中最常见的字母到最不常见的字母
【发布时间】:2019-09-21 11:46:35
【问题描述】:

我正在尝试应用密码方法。 我试过做一个直方图,但我没有在 main 中做 argc argv 因为我直接从那里测试它,我只是调用 ./a.out:

    #include <stdio.h>                                                              
    #include <string.h>                                                             

     void cipher(const char text[], const char table[])                       
     {                                                                               
         int length = strlen(text);                                                 
         int hist[26];    //histogram for each letter of the alphabet                                        
         for (int i = 0; i < 26; i++)                                    
         {                                                                           
             hist[i] = 0;                                                            
         }                                                                           
         char startletter;                                                           
         for (const char *temp = text; *temp != '\0'; temp++)                        
         {        
             startletter = *letter;                                                                   
             for (const char *letter = temp; *letter != '\0'; letter++)              
             {                                                                                                                     
                 if (*letter == startletter)                                         
                 {                                                                   
                     hist[*letter - 65] += 1;                                        
                 }                                                                   
             }                                                                       
         }                                                                           
         for (int i = 0; i < 26; i++)                                            
         {                                                                           
             printf("%d ", hist[i]);                                                 
         }                                                                           
     }       

输出:

0 0 0 0 0 15 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 3 1 0 0

(现在应该打印出string1中出现的字母)

【问题讨论】:

  • 我已经更正了我的代码,我将编辑它现在编译(但给出错误的输出)
  • 我做错了什么
  • @aschancla 这是什么 hist[i] =;?
  • 抱歉有人编辑了我的代码,我过一会再编辑
  • 刚刚修改的代码

标签: c arrays string pointers histogram


【解决方案1】:

代码如下:

#include <stdio.h>
#include <string.h>

 void cipher(const char text[], const char table[]){
    int hist[26];    //histogram for each letter of the alphabet
    for (int i = 0; i < 26; i++)
         hist[i] = 0;

    for (const char *temp = text; *temp != '\0'; temp++)
         hist[*temp-65] += 1;

    for (int i = 0; i < 26; i++){
         if (hist[i])
            printf("%c=%d\n", 'A'+i, hist[i]);
       }
 }

 int main(void){
     const char a[] = "FXOWFFOWOFF";
     const char b[] = "ABCD";
     cipher(a, b);
     return 0;
 }

您所要做的就是删除嵌套循环。

【讨论】:

  • 谢谢,现在我想我需要:在直方图中找到最大值,打印字母并将其设置为 0 ?
  • @laschancla 是的,通过表格。
【解决方案2】:

您需要对直方图进行排序,然后根据排序后的直方图打印表格中的字母

代码如下:

#include <stdio.h>
#include <string.h>

void cipher(const char text[], const char table[])
{
    int length = strlen(text);

    int hist[26];    //histogram for each letter of the alphabet
    for (int i = 0 ; i < 26; i ++)
    {
        hist[i] = 0;
    }
    for (const char *temp = text; *temp != '\0'; temp++)
    {
        hist[(int)*temp - 65]++;
    }
    for (int i = 0; i < 26; i++)
    {
        printf("%d ", hist[i]);
    }
    printf("\n");

    // sort
    int sort[26];
    for (int i = 0; i < 26; ++i)
    {
        sort[i] = i;
    }
    int tempint;
    for (int i = 0; i < 25; ++i)
    {
        for (int j = i; j < 26; ++j)
        {
            if (hist[sort[j]] > hist[sort[i]]) {
                tempint = sort[j];
                sort[j] = sort[i];
                sort[i] = tempint;
            }
        }
    }

    for (int i = 0; i < strlen(table); ++i)
    {
        printf("%c %c\n", (char)(65 + sort[i]), table[i]);
    }
    return;
}

int main(void)
{
    const char a[] = "FXOWFFOWOFF";
    const char b[] = "ABCD";
    cipher(a, b);
    return 0;
}

【讨论】:

  • 谢谢,但为什么我们需要排序 hist ?我们能找到最大值并打印 hist[i] + '0' 吗?然后将最大值设置为0并重新启动
猜你喜欢
  • 2022-11-13
  • 2018-06-13
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2017-10-25
  • 2019-11-01
相关资源
最近更新 更多