I am doing code for CAESAR CIPHER. I need to built a histogram or frequency distribution for the output. I thought that finding out the frequency of the ASCII code, then print the frequency with * by using loop. But my code din't have array, can't do ++freq[c[i]].
I have no idea. Anyone can help?

#include <stdio.h>
#include <iostream.h>
void encrypt(int shift);

int main()
{
    int shift;
    int decOrEnc;

    printf("Enter shift amount (1-25): ");
    scanf("%d", &shift);

    if (shift < 0)
    {
        printf("Bad Input.");
        return 0;
    }

    printf("Type 1 to Encrypt or 0 to Decrypt: ");
    scanf("%d", &decOrEnc);

    if (decOrEnc != 0 && decOrEnc !=1)
    {
        printf("Bad Input.");
        return 0;
    }
    while(getchar() != '\n');
    if (decOrEnc == 1)
        encrypt(shift);
    else
    {
        shift = -1 * shift;
        encrypt(shift);
    }
    getchar();
}

void encrypt(int shift)
{
    char ch;
    printf("Please Enter Your ID : ");
    ch = getchar();
    while(ch != '\n')
    {
        if (ch == ' ')
            putchar(ch);
        else
        {
            if(shift == 1)
            putchar(ch + shift);
            else
            putchar(ch - shift);
        }
        ch = getchar();
    }

    {
       unsigned int c;
       while (1)
       {
           printf("\n" ); 
           c = getchar();
           printf("%c - %d", c, c );
       }
    }
   /* int freq[100]={0};
    for(int i=0; i<127; i++)
    {
            ++freq[c[i]];
    }*/
    system("pause");
    putchar(ch);

}

Recommended Answers

All 2 Replies

For your program are you only using letters from the English alphabet? If so, you can create your freq array as an array of 26. To convert from ASCII to letters, simply subtract 65 from upper case letters or 97 from lower case letters.
To do this, make sure you're actually dealing with a letter. You can use the isalpha function from the types.h header.
In fact, this header also has a toupper function (to upper-case) that you can use to convert the lower case letters.
Once you have this, you can subtract 65 from the resulting value to give you an index value that ranges from [a=0...z=25].

Happy coding!

commented: oo~ Understand!! thanks!!! ^^ +0

Correction: That's the ctypes.h header. I apologize.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.