Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

frequency of letters..??

da_comp_guyda_comp_guy Member Posts: 37
hey ppl..listen got a small prob here..i wrote a program which scans for the frequencies of the letters in it..but i'm kinda confused. first take a look at the code:

#include
#include
#include

struct ftable
{
char ch;
unsigned long int freq;

ftable()
{ ch=0;freq=0;}

};

char text[256];
ftable t[256];
main()
{
int i,j;
cout<<"Enter the text: ";
gets(text);

//analysing the text
for(i=0;text[i]!=NULL;i++)
{ t[i].ch=text[i];
for(j=0;text[j]!=NULL;j++)
{ if(t[i].ch==text[j])
t[i].freq++;
}
}

cout<<"Frequencies:
";
for(i=0;text[i]!=NULL;i++)
{

cout<<t[i].ch<<" : "<<t[i].freq;
cout<<endl;

}

system("PAUSE");
}

Well here..everything works pretty fine , except for the output part. wat happens is ,if the letter repeats in the given text...the output also repeats that many times! can anyone fix it?? and moreover is it the correct way for frequency analysis...if not..plzz temme the correct thing! thanx!
"[b]The[blue] GEEK[/blue] Shall Inherit The Earth" ;-) [/b]

Comments

  • DonotaloDonotalo Member Posts: 715
    Your code:
    [code]
    struct ftable
    {
    char ch;
    unsigned long int freq;

    ftable()
    {ch=0;freq=0;}
    };

    char text[256];
    ftable t[256];

    void main()
    {
    int i,j;
    cout<<"Enter the text: ";
    gets(text);

    //analysing the text
    [red] for(i=0;text[i]!=NULL;i++)
    {
    t[i].ch=text[i];
    for(j=0;text[j]!=NULL;j++)
    {
    if(t[i].ch==text[j])
    t[i].freq++;
    }
    }
    [/red]
    cout<<"Frequencies:
    ";
    for(i=0;text[i]!=NULL;i++)
    {
    cout<<t[i].ch<<" : "<<t[i].freq;
    cout<<endl;
    }
    }
    [/code]
    The red statements are mainly responsible for repeatation. if a string like "ABCAB" is input, then it will count every time A, B, C AND A, B appear, and in the last loop, show them all.
    [code]
    char text[256];
    ftable t[256];
    [/code]
    In the table, u r keeping all of the charachters given as input, but u want particularly how many times a charachter appear in the [blue]text[/blue]. since there are 26 upper case + 26 lower case letters, u can declare ftable like this:
    [code]
    ftable t[52];
    [/code]
    Now, ur ftable structure should be modified so that u can keep track whether a given input character is appear or not. here is that:
    [code]
    struct ftable
    {
    bool isappear;
    char ch;
    unsigned long int freq;

    ftable()
    {isappear = false; ch=0;freq=0;}
    };
    [/code]
    Whenever u see a character appears in the text, simply make [blue]isappear true[/blue]. here is the complete code (include necessary files):
    [code]
    struct ftable
    {
    bool isappear;
    char ch;
    unsigned long int freq;

    ftable()
    {isappear = false; ch=0;freq=0;}
    };

    char text[256];
    ftable t[52];
    [green]//'A' = 65 'Z' = 90 'a' = 97 'z' = 122[/green]
    void main()
    {
    int i,j,p;
    cout<<"Enter the text: ";
    gets(text);
    [green] //fgets(text, sizeof(text), stdin);[/green]

    [green] //analysing the text[/green]
    for(i=0;text[i]!=NULL;i++)
    {
    if (!isalpha(text[i])) continue;
    p = int(text[i]) - int('A');
    if (p >= 26)[green]//if text[i] be a lower case letter[/green]
    p -= 7;[green]//int('a') equals to 97[/green]
    if (t[p].isappear) continue;

    t[p].ch=text[i];
    t[p].isappear = true;
    for(j=0;text[j]!=NULL;j++)
    {
    if(t[p].ch==text[j])
    t[p].freq++;
    }
    }

    cout<<"Frequencies:
    ";
    for(i=0; i < 52;i++)
    {
    if (t[i].isappear) {
    cout<<t[i].ch<<" : "<<t[i].freq;
    cout<<endl;
    }
    }
    system("PAUSE");
    }
    [/code]
    i've commented out [blue]fgets(...);[/blue]. try to use [blue]fgets[/blue] instead of [blue]gets[/blue], since [blue]fgets[/blue] will not cause array overrun.
  • da_comp_guyda_comp_guy Member Posts: 37
    hey thnx a lot..i never thought of using bool..anyways i dint want to scan only for letters...it can be any thing, any symbol or numbers. actually i'm thinking of a new encryption algorithm....which uses frequencies etc. thanx agin :)
    "[b]The[blue] GEEK[/blue] Shall Inherit The Earth" ;-) [/b]

Sign In or Register to comment.