Thread: Need help with small program

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    22

    Question Need help with small program

    Hello all,
    I've been trying to solve a problem here but I'm not able to get the concept right. Please help me out.
    Here is the question and the answer is written below.

    Q. Write a computer program to tabulate the total occurances of
    each of the alphabets A to Z in a string. Allocate a large
    array of 100 or more characters for the input. Use gets function
    get a string.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int main()  {
      int a[100],i, b[100],j;
      printf("Enter the sentence:");
      gets(a,100);
      b[26]={97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123}
    
    for(i=0;i<100;i++){
      for(j=0;j<=26;j++){
        if(a[i]==b[j]){
          if(b[j]==97)
            ac++;
          elseif(b[j]==98)
            bc++;
          elseif(b[j]==99)
            cc++;
          elseif(b[j]==100)
            dc++;
          elseif(b[j]==101)
            ec++;
          elseif(b[j]==102)
            fc++;
          elseif(b[j]==103)
            gc++;
          elseif(b[j]==104)
            hc++;   
      getch();
      return 0;
    }
    Thanks in advance
    Last edited by alokrsx; 05-24-2009 at 12:50 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ac++;
    You don't declare these.

    Have you considered an array of counters?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by alokrsx View Post
    Hello all,
    I've been trying to solve a problem here but I'm not able to get the concept right. Please help me out.
    Here is the question and the answer is written below.

    Q. Write a computer program to tabulate the total occurances of
    each of the alphabets A to Z in a string. Allocate a large
    array of 100 or more characters for the input. Use gets function
    get a string.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int main()  {
      int a[100],i, b[100],j;
      printf("Enter the sentence:");
      gets(a,100);
      b[26]={97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123}
    
    for(i=0;i<100;i++){
      for(j=0;j<=26;j++){
        if(a[i]==b[j]){
          if(b[j]==97)
            ac++;
          elseif(b[j]==98)
            bc++;
          elseif(b[j]==99)
            cc++;
          elseif(b[j]==100)
            dc++;
          elseif(b[j]==101)
            ec++;
          elseif(b[j]==102)
            fc++;
          elseif(b[j]==103)
            gc++;
          elseif(b[j]==104)
            hc++;   
      getch();
      return 0;
    }
    Thanks in advance
    You're close, but not quite there.

    Say you had an array like count[] that was all zero's, initially.

    Now for every char in the string. (a for or while or do while loop)

    12345]* if the char is a letter A-Z or a-z, change it to all upper case.
    12345]* Now we want 'A' to correspond to count[0], and here's the heart of the trick,
    12345]* so we *subtract* an 'A' from each letter, and then:

    Code:
    count[letter]++;
    } //end of for,while or do while loop

    Now every A letter in the string will increment count[0], and every B will do the same for count[1], and C's will increment count[2], etc.

    //now print your count[] array remembering to add 'A' to the index number, as you display the results, and you are printing a char, not a digit, in your printf() line of code.
    Last edited by Adak; 05-24-2009 at 04:29 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    22
    Thanks for your reply, Adak.

    I do not know how to convert lower case letter to upper case other than using the if statement which will be too long.

    Sorry, but I'm still not able to understand

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You already know about ASCII values. You can just add or subtract from those to change case.

    Also, there are toupper() and tolower() macros in ctype.h (*nix).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    22
    Got it.
    Thanks a lot for your help everyone.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<string.h>
    
    int main()
    {
      int i;
      int nAs = 0;
      int nBs = 0;
      int nCs = 0;
      int nDs = 0;
      int nEs = 0;
      int nFs = 0;
      int nGs = 0;
      int nHs = 0;  
      int nIs = 0;
      int nJs = 0;
      int nKs = 0;
      int nLs = 0;
      int nMs = 0;
      int nNs = 0;
      int nOs = 0;
      int nPs = 0;
      int nQs = 0;  
      int nRs = 0;
      int nSs = 0;
      int nTs = 0;
      int nUs = 0;
      int nVs = 0;
      int nWs = 0;
      int nXs = 0;
      int nYs = 0;
      int nZs = 0;  
      char s1[100];
      printf("Enter the string: ");
      scanf("%s",s1);
      for(i=0; i<strlen(s1); i++)
        {
          if (s1[i] == 'a' || s1[i] == 'A')
            nAs++;
          else if (s1[i] == 'B' || s1[i] == 'b')
            nBs++;
          else if (s1[i] == 'C' || s1[i] == 'c')
            nCs++;
          else if (s1[i] == 'D' || s1[i] == 'd')
            nDs++;
          else if (s1[i] == 'E' || s1[i] == 'e')
            nEs++;
          else if (s1[i] == 'F' || s1[i] == 'f')
            nFs++;
          else if (s1[i] == 'G' || s1[i] == 'g')
            nGs++;
          else if (s1[i] == 'H' || s1[i] == 'h')
            nHs++;
          else if (s1[i] == 'I' || s1[i] == 'i')
            nIs++;  
          else if (s1[i] == 'J' || s1[i] == 'j')
            nJs++;
          else if (s1[i] == 'K' || s1[i] == 'k')
            nKs++;
          else if (s1[i] == 'L' || s1[i] == 'l')
            nLs++;
          else if (s1[i] == 'M' || s1[i] == 'm')
            nMs++;
          else if (s1[i] == 'N' || s1[i] == 'n')
            nNs++;
          else if (s1[i] == 'O' || s1[i] == 'o')
            nOs++;
          else if (s1[i] == 'P' || s1[i] == 'p')
            nPs++;
          else if (s1[i] == 'Q' || s1[i] == 'q')
            nQs++;  
          else if (s1[i] == 'R' || s1[i] == 'r')
            nRs++;
          else if (s1[i] == 'S' || s1[i] == 's')
            nSs++;
          else if (s1[i] == 'T' || s1[i] == 't')
            nTs++;
          else if (s1[i] == 'U' || s1[i] == 'u')
            nUs++;
          else if (s1[i] == 'V' || s1[i] == 'v')
            nVs++;
          else if (s1[i] == 'W' || s1[i] == 'w')
            nWs++;
          else if (s1[i] == 'X' || s1[i] == 'x')
            nXs++;
          else if (s1[i] == 'Y' || s1[i] == 'y')
            nYs++;
          else if (s1[i] == 'Z' || s1[i] == 'z')
            nZs++;     
        }
      printf("\n # of A: ",nAs);
      printf("\n # of B: ",nBs);
      printf("\n # of C: ",nCs);
      printf("\n # of D: ",nDs);
      printf("\n # of E: ",nEs);
      printf("\n # of F: ",nFs);
      printf("\n # of G: ",nGs);
      printf("\n # of H: ",nHs);
      printf("\n # of I: ",nIs);
      printf("\n # of J: ",nJs);
      printf("\n # of K: ",nKs);
      printf("\n # of L: ",nLs);
      printf("\n # of M: ",nMs);
      printf("\n # of N: ",nNs);
      printf("\n # of O: ",nOs);
      printf("\n # of P: ",nPs);
      printf("\n # of Q: ",nQs);
      printf("\n # of R: ",nRs);  
      printf("\n # of S: ",nSs);
      printf("\n # of T: ",nTs);
      printf("\n # of U: ",nUs);
      printf("\n # of V: ",nVs);
      printf("\n # of W: ",nWs);
      printf("\n # of X: ",nXs);
      printf("\n # of Y: ",nYs);
      printf("\n # of Z: ",nZs);
      getch();
      return 0;  
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I guess it's a solution, but it's not very practical or neat. As suggested, use an array for the count, and use tolower or toupper, and then use math to index the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    The loop may look something like this...

    Code:
      for(i=0; i<strlen(s1); i++)
        {
            for(j='a'; j< 'z'; j++)
            {
                if(s1[i] == j)
                    alphabets[j-'a']++;
            }
        }
    prior to this use a loop for upper case to lower case and after this use a loop for printf statement ... that will look better I guess ...

    Edesign

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a reason we just didn't give them the source code version of the answer you know. It's not that we couldn't do it. It's that we wanted them to try it on their own first.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    Quote Originally Posted by quzah View Post
    There's a reason we just didn't give them the source code version of the answer you know. It's not that we couldn't do it. It's that we wanted them to try it on their own first.


    Quzah.

    I am no one to think that you people can't write it ... I have learned everything on C board and I would encourage other people to do so .. the only reason I posted that piece of code is that after getting the 'solution' , it was much likely that alokrsx wouldn't bother to look at suggestions made before ...

    perhaps by looking at that small piece of code he can realize how brief and neat it can be ...

    Anyways I'll consider your point before posting now on ...

    Edesign

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help with small program
    By cjohnman in forum C Programming
    Replies: 11
    Last Post: 04-14-2008, 09:59 AM
  2. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 10:52 AM
  3. Help writing small program
    By guitarhead2000 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2004, 12:42 PM
  4. A little Help with a small program
    By whtpirate in forum C Programming
    Replies: 7
    Last Post: 06-05-2003, 06:15 PM
  5. Very small program...Whats wrong???
    By SmokingMonkey in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2003, 09:09 PM