Need a few pointers in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • henno2000
    New Member
    • Nov 2006
    • 1

    Need a few pointers in C

    Hi, after a little help if possible please folks

    I am wanting to create a program that accepts an input of characters using gets(), then analyse the string and count the occurance of each letter of the alphabet.

    Is it possible to do this in one function, or would I have to repeat it 26 times?

    Also, to attain this function, I was going to construct it from an IF statement that is nested in a WHILE loop.

    Is the the best way of appraoching this problem, or is there a simpler solution?

    Many thanks in advance
  • cuteanu
    New Member
    • Oct 2006
    • 14

    #2
    Use a for loop from a to z and add up the no of times each alphabet occurs,
    u will need a double for loop

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by cuteanu
      Use a for loop from a to z and add up the no of times each alphabet occurs,
      u will need a double for loop
      I suggest you use 2 functions
      The first one takes a char array and a char and returns the position of this character in the array. If this is called say getPosition, then


      in the other function or in main
      Define a char[] called alphabet and initialise it with the 26 characters.
      Define an int[] called frequencies of size 26 and initialise it with zeros.
      You would use a for loop as suggested but it would not go from a to z but from 0 to the number of characters entered.
      at each step of the for loop you do
      frequencies[getPosition(alp habet, character)] = frequencies[getPosition(alp habet, character) ] + 1;

      where character is the input character

      Comment

      • cuteanu
        New Member
        • Oct 2006
        • 14

        #4
        try this:

        include- iostream, stdio,string.h,
        char n[50];
        char x='A';
        char s='a';
        int f=0;
        gets(n)
        while (x<='Z' && s<='z')
        {
        for (int y=0;y<=strlen(n );y++)
        {
        if (x==n[y]||s==n[y])
        f++;
        }
        cout<<"frequenc y of "<<x<<"is"< <f;
        f=0;
        x++;
        s++;
        }

        Comment

        Working...