C - Заменить от самых распространенных букв в строке1 из строки2 - PullRequest
0 голосов
/ 21 сентября 2019

Я пытаюсь применить метод шифрования.Я попытался сделать гистограмму, и я не сделал 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]);                                                 
         }                                                                           
     }       

output:

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)

Ответы [ 2 ]

0 голосов
/ 21 сентября 2019

вам нужно отсортировать гистограмму и затем распечатать буквы из таблицы в соответствии с отсортированной гистограммой

вот код:

#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;
}
0 голосов
/ 21 сентября 2019

Вот код:

#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;
 }

Все, что вам нужно было сделать, это удалить вложенный цикл.

...