Thread: Counting Characters And Words

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    10

    Counting Characters And Words

    hi
    i have a problem
    i need to do this
    for example
    i will write
    welcome.to programming.welcome
    then it will show me this
    we use "w" for 21 times
    we use "e" for 4 times
    we use "l" for 2 time.
    etc.
    and it will write
    "welcome" used for 2 times

    in summary it will count the letters if how many letters same for each letters and how many words are same in each words.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    10
    i searched ı couldn't find the answer

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Did you find any code to start with so that you can post an initial attempt?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    10
    i wrote counting letters. but ı did it with "goto" function ı must learn writing without "goto".
    and ı couldn't find how to write counting words as my first messege
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int i,j,say;
    main(){
    char isim[20];
    
    clrscr();
    printf("write sentence = "); 
    
    scanf("%s",isim);
    
    for(i=0;i<strlen(isim);i++)
    { 
    for(j=0;j<i;j++){
    
    if(isim[i]==isim[j] || isim[i]==isim[j]+32 ||isim[i]==isim[j]-32) goto atla;}
    
    for(j=i+1;j<strlen(isim);j++) {
    
    if(isim[i]==isim[j] || isim[i]==(isim[j]+32) || isim[i]==isim[j]-32)
    
    say=say+1;
    
    }
    
    printf("%c harfinden = %d tane var.\n",isim[i],say+1); say=0; atla:
    }
    
    getch();
    
    return 0;
    
    }
    PS:"say" means >count
    "isim" means >word
    "atla" means jump
    Last edited by SNaRe; 05-04-2005 at 10:24 AM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Let's try to get you started a little better.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
       char sentence[] = "welcome.to programming.welcome";
       int  i, count[CHAR_MAX] = {0}; /* initialize letter count */
       /*
        * Go through the sentence and increment the letter count for each letter.
        */
       for ( i = 0; sentence[i]; i++ )
       {
          ++count [ sentence[i] ];
       }
       /*
        * Display the results.
        */
       puts(sentence);
       for ( i = 0; i < CHAR_MAX; i++ )
       {
          if ( count[i] )
          {
             printf("'%c' : %d\n", i, count[i]);
          }
       }
       return 0;
    }
    
    /* my output
    welcome.to programming.welcome
    ' ' : 1
    '.' : 2
    'a' : 1
    'c' : 2
    'e' : 4
    'g' : 2
    'i' : 1
    'l' : 2
    'm' : 4
    'n' : 1
    'o' : 4
    'p' : 1
    'r' : 2
    't' : 1
    'w' : 2
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    10
    i must learn that <limits.h> it makes works easier ı must work on words now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help counting characters
    By dom89 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 10:25 PM
  2. Counting characters
    By jmajeremy in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2006, 10:14 AM
  3. Counting Number of Words in a Text File Using C
    By wvu2005 in forum C Programming
    Replies: 16
    Last Post: 09-27-2005, 11:45 AM
  4. help with counting words
    By geo_c in forum C Programming
    Replies: 7
    Last Post: 08-23-2004, 06:53 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM