简体   繁体   English

如何将字符串划分为子字符串并分配另一个子字符串?

[英]How to divide a string to substring and assign another substring?

I want to divide *eString to substrings. 我想将* eString划分为子字符串。 Substrings should be like that: 子串应该像这样:

y_{1} = y_{1}y_{m+1}y_{2m+1}...
y_{2} = y_{2}y_{m+2}y_{2m+2}...
y_{m} = y_{m}y_{2m}y_{3m}...

where y is the element of *eString, and y is the substring of these elements. 其中y是* eString的元素,而y是这些元素的子字符串。

For instance, if an user expects the key length which is 5, there should be (string size / 5) substrings. 例如,如果用户期望密钥长度为5,则应该有(字符串大小/ 5)子字符串。 y_{1} has to contain the fist element of each divided substring. y_{1}必须包含每个分割的子字符串的第一元素。 So, how can I implement this? 那么,我该如何实现呢?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ALPHA 26


char *ReadFile(char *);


int main(int argc, char *argv[])
{
    double frequency[ALPHA] = {0};
    int c = 0;
    int keylen = 0;
    int counter = 0;
    double indexofCoincidence = 0,total = 0;

    const char *eString = ReadFile("cipher.txt");
    int len = 0;
      if (eString) {
        puts("The encrypted text is:");

        puts(eString);
        puts("");

        len = strlen(eString);
        printf("The length of text is %d\n",len);
      }
   puts("");

      while(eString[c]!= '\0'){

         if(eString[c]>= 'a' && eString[c]<='z')
             frequency[eString[c]-'a']++;
        c++;
      }
puts("The letters frequencies are :\n");
      for(c=0; c<ALPHA;c++){
           if(frequency[c]!= 0)
                printf("%c : %.3f\t",c+'a',(frequency[c]/len));

           total += (frequency[c]*(frequency[c]-1));

      }

        indexofCoincidence = (total/((len)*(len-1)));

printf("\n\nIndex of Coincidence : %.3f\n",indexofCoincidence);

        if(indexofCoincidence < 0.060){

                 printf("\nIt looks like randomly.\n");

        }
    printf("Enter the your expected key length : ");
    scanf("%d",keylen);
    printf("\n");

    char *y;
       while(counter != keylen)
       {
           for(int i = 0; i<(len/keylen);i++){
             y[counter] = *eString();
           }
         counter++
       }


  return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
    char *eString = "The quick brown fox jumps over the lazy dog";
    int keylen = 5;
    int len = strlen(eString);
    int y_len = (len + keylen) / keylen + 1;
    int i,j;
    char **y = malloc(keylen * sizeof(*y));

    for(i=0; i < keylen; ++i){
        y[i] = malloc(y_len * sizeof(**y));
    }
    char *p = eString;
    i = j = 0;
    while(*p){
        y[i % keylen][j] = *p++;
        y[i % keylen][j+1] = 0;
        if(++i % keylen == 0)
            ++j;
    }

    //check print & deallocate
    for(i = 0; i < keylen; ++i){
        printf("y_{%d} : %s\n", i+1, y[i]);
        free(y[i]);
    }
    free(y);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM