User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,570 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,604 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2859 | Replies: 13 | Solved
Reply
Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

Help Program calculate letter occurence in a sentence

  #1  
Oct 20th, 2007
Please help~ i stuck in this question so long time... This assignment is need to hand up due tomorrow >.<''' . Once again i confuse about array. Please correct me and show me some tips ya. Any of ur attention will be 'God Bless You'.
Here is my coding>>>
  1. #include<stdio.h>
  2.  
  3. void main()
  4. {
  5. char str[51];
  6. char letter[27]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  7. int count, i;
  8.  
  9. printf("Enter sentence or phrase>>>");
  10. fgets(str, 51, stdin);
  11. for(count=0;str[count]!='\0';count++)
  12. {
  13. for(i=0; letter[i]; i++)
  14. if(letter[i]==str[count])
  15. letter[i]++;
  16. }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 236
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 21
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Program calculate letter occurence in a sentence

  #2  
Oct 20th, 2007
			if(letter[i]==str[count])
Think about what you are doing here. You're saying something to the effect of, "if the i-th element of letter[] equals the count-th element of str[] ...". Since you've initialized letter[] to 'a', 'b', ... this works the first time. But then you go and increment letter[i]. So, say the letter was a 'b'. letter[] now contains 'a', 'c', 'c', 'd', .... Likely not what you wanted.

Your trouble is that you need a separate array to count the occurrences of the letters. You can't just increment letter[i], because letter[] is your lookup array. Its contents can't change.

So I suggest that you create another array, say:
int num[27] = {0};
The ={0} code just initializes each element of num[] to zero, which is what you want, because you can then start counting some letters.

This would work, but you don't need the letter[] array at all. But that's another post.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote  
Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

Re: Program calculate letter occurence in a sentence

  #3  
Oct 20th, 2007
Thx dwks. Can you show further the algorithm of this program for me to work on?
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 236
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 21
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Program calculate letter occurence in a sentence

  #4  
Oct 20th, 2007
Something like this. I've glossed over details that you seem to already understand.
  • Declare an array to hold the letter counts one element for each letter, with each element initialized to zero.
  • Read in a string from the user.
  • For every letter in this string:
    • If a character in the string is a letter:
      • Increment the corresponding element in the letter count array.

Okay, so that was a little difficult to read. Let me try that again.

The easiest way to do this is probably to count the number of occurances of every character you could possibly come across. For example:
int num[256] = {0}, count;

for(count=0;str[count]!='\0';count++)
{
    num[str[count]] ++;
}
Then, extract the counts that you care about. For this, use the function isalpha() from <ctype.h>, which returns true if its argument is an alphabetic letter, lowercase or uppercase. (There's also islower() and isupper() for lowercase and uppercase letters only.)

Then, if the count you're interested in is the count of a letter, print it out.
for(x = 0; x < 256; x ++) {
    if(isalpha(x)) {
        /* count[x] is a count of a letter of some sort */
    }
}
You might also want to only handle the counts that are non-zero.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote  
Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

Re: Program calculate letter occurence in a sentence

  #5  
Oct 20th, 2007
Sorry i never learn about <ctype.h>, isalpha, islower and isupper, is there other way??? My teacher probably wouldn't accept those. And about this statement, i not really get it >>num[str[count]] ++;<<
Thx for help.
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Program calculate letter occurence in a sentence

  #6  
Oct 20th, 2007
if (islower(x))
can be written
if (x >= 'a' and x <= 'z')
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Dec 2006
Posts: 1,569
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 12
Solved Threads: 114
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: Program calculate letter occurence in a sentence

  #7  
Oct 21st, 2007
Never define main as void return. Start defining main as int main( void ) or int main ( int argc, char *argv[] ) if you are expecting arguments from the command line.

>Sorry i never learn about <ctype.h>, isalpha, islower and isupper, is there other way???
  1. #include <stdio.h>
  2. /* #define ALL_CHARACTER */ /* enable for use all characters in RANGE */
  3. /* #define RANGE */ /* enable for only lower case */
  4. #ifdef RANGE
  5. #define START 'a'
  6. #endif
  7. #ifndef RANGE
  8. #define START 'A'
  9. #endif
  10.  
  11. int main( void )
  12. {
  13. char character[256] = { 0 };
  14. int c = '\0';
  15.  
  16. puts( "Enter some text:" );
  17.  
  18. while ( ( c = getchar() ) != '\n' && c != EOF )
  19. {
  20. ++character[c];
  21. }
  22. for ( c = START; c <= 'z'; c++ )
  23. {
  24. #ifndef ALL_CHARACTER
  25. if ( character[c] )
  26. #endif
  27. printf( "%c = %d\n", c, character[c] );
  28. }
  29. getchar();
  30. return 0;
  31. }
Last edited by Aia : Oct 21st, 2007 at 1:02 am.
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Reply With Quote  
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 1,018
Reputation: ithelp will become famous soon enough ithelp will become famous soon enough 
Rep Power: 6
Solved Threads: 68
ithelp ithelp is online now Online
Veteran Poster

Re: Program calculate letter occurence in a sentence

  #8  
Oct 21st, 2007
Cannot you use std::string apis ?
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Program calculate letter occurence in a sentence

  #9  
Oct 21st, 2007
>Cannot you use std::string apis ?
This is not the c++ forum.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
int main( void )
{
   int lettercount[26] = {0};
   char letterz[] = {"abcdefghijklmnopqrstuvwxyz"};

   char text[100];
   fputs("enter some text: ", stdout);
   fflush(stdout);
   if ( fgets(text, sizeof text, stdin) != NULL )
   {
      char *newline = strchr(text, '\n'); /* search for newline character */
      if ( newline != NULL )
      {
         *newline = '\0'; /* overwrite trailing newline */
      }
      printf("text = \"%s\"\n", text);
      
      int i;
      int j;
      int wordLength = strlen( text ); /*get the number of characters in the sentence*/
      for ( i = 0; i < wordLength; i++ )
      {
        for ( j = 0; j < 26; j++ )
        {
          if ( tolower( text[i] ) == letterz[j] ) /*tolower convert uppercase to lowercase*/
          {
            lettercount[j]++;
          }
        }
      }
   }
   
/*print out results*/
   int k;
   for ( k = 0; k < 26; k++ )
   {
     printf( "%c:%d ", letterz[k], lettercount[k] );
     printf("\n");
   }
   return 0;
}

Pay attention to the following tutorial about reliably getting input from the user:
http://www.daniweb.com/tutorials/tutorial45806.html
Last edited by iamthwee : Oct 21st, 2007 at 11:31 am.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Oct 2007
Posts: 26
Reputation: bobei89 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bobei89's Avatar
bobei89 bobei89 is offline Offline
Light Poster

Re: Program calculate letter occurence in a sentence

  #10  
Oct 21st, 2007
Originally Posted by WaltP View Post
if (islower(x))
can be written
if (x >= 'a' and x <= 'z')



Okay i got it. Thankyou
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:57 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC