Count the number of words in a string

Source: Internet
Author: User

 


Requirement: enter a string to count the number of words. Words are separated by spaces. Multiple spaces are allowed. Algorithm .

Example: input: I love China
Output:
I:1
Love:2
China:1

 

 

First, we thought of a simulated method. We used struct to cache the words that have appeared, and then input the text to traverse the new words to traverse the struct, check whether the word has been saved for related processing.
If n letters are in the input text and m are not repeated, the algorithm complexity is O (NM ^ 2), preferably M = 1, the worst case is M = n.CodeAs follows:

 

 1 # Include <stdio. h>2 # Include < String . H> 3     Struct  Struct_words {  4            Char Word [ 20  ];  5            Int  Count;  6   };  7     Int Main (){  8            Char   String [ 100  ];  9            Char  C;  10            Struct Struct_words words [ 20  ];  11            Int I = 0 , K =0 , Ws = 0  ;  12    13            For (; I < 20 ; I ++ ){  14 Words [I]. Word [ 0 ] = '  \ 0  '  ;  15 Words [I]. Count =0  ;  16   }  17 Puts ( "  Please input words.  "  );  18 Gets ( String  );  19 Puts ( "  =============== Start to take the word ============================  " );  20    21 I = 0  ;  22            Do  {  23 C = String  [I];  24                    If (C! = '   ' & C! = ' \ 0  '  ){  25 Words [K]. Word [WS] = C;  26 Words [K]. Count = 1  ;  27 WS ++ ;  28 } Else  {  29 Words [K]. Word [WS] ='  \ 0  '  ;  30 Ws = 0  ;  31 K ++ ;  32   }  33 I ++ ;  34 } While (C! ='  \ 0  '  ); LDA  35    36    37 Puts ( "  ============= Merge the same words ================  "  );  38            For (I = 0 ; Words [I]. Word [ 0 ]! = ' \ 0  ' ; I ++ ){  39   Puts (words [I]. Word );  40                    If (Words [I]. Count> = 1  )  41                    For (K = I; words [K]. Word [ 0 ]! = '  \ 0  ' ; K ++){  42                            If (Strcmp (words [I]. Word, words [K]. Word) = 0  43 & Words [K]. Count = 1  ){  44 Words [K]. Count -- ;  45 Words [I]. Count ++ ;  46   } 47   }  48   }  49    50 Puts ( "  ==================== End ==================  "  );  51            For (I = 0 ; Words [I]. Word [ 0 ]! = ' \ 0  ' ; I ++ ){  52                    If (Words [I]. Count! = 0  )  53 Printf ( "  % S: \ t % d \ n  "  , Words [I]. Word, words [I]. Count );  54   }  55            Return ( 0  );  56 }

 

 

 

Then, to optimize the word cache and the statistics on the number of occurrences of words, you must traverse the user input text. However, the hash algorithm can be used to optimize the word cache and the number of occurrences, by leveraging the features of the hash algorithm, the complexity is immediately reduced to O (n). The implementation code is as follows:

 

 

 1 # Include <stdio. h> 2 # Include < String . H> 3   # Define N 100 4   5   Struct Struct_words {  6       Char Word [ 100  ];  7       Int  Count;  8   };  9   10   Int Hash ( Char * Key)  11   { 12 Unsigned Long H = 0  ;  13         While (* Key)  14   {  15 H = (H < 4 ) + * Key ++ ;  16 Unsigned Long G = H &0xf0000000l  ;  17                        If  (G)  18 H ^ = G> 24  ;  19 H & = ~ G;  20   }  21          Return H & N; 22   }  23   Int  Main (){  24       Char   String [ 1000  ];  25       Char Current_word [ 100  ];  26       Char  C; 27       Struct Struct_words words [ 200  ];  28       Int I = 0 , K = 0 , Ws = 0  , Key;  29       Int Keys [ 100  ];  30   31      For (; I < 200 ; I ++ ){  32 Words [I]. Word [ 0 ] = '  \ 0  '  ;  33 Words [I]. Count = 0  ;  34   }  35 Puts ("  ============== Enter some words and separate them with spaces ==========================  "  );  36 Gets ( String  );  37   38 I = 0  ;  39       Do  {  40 C = String [I];  41           //  If there is a space before the first word, skip  42           If (Ws = 0 & C = '   ' ) {I ++; Continue  ;}  43           If (C! = '   ' & C! = ' \ 0  '  ){  44 Current_word [WS] = C;  45 WS ++ ;  46 } Else  {  47 Current_word [WS] = '  \ 0  '  ;  48 Key = Hash (current_word );  49               If (Words [Key]. Count = 0  ){  50   Strcpy (words [Key]. Word, current_word );  51 Keys [k] = Key;  52 K ++ ;  53   }  54 Words [Key]. Count ++ ;  55 Ws = 0  ;  56   }  57 I ++ ;  58 } While (C! = '  \ 0  '  );  59   60 Printf ( "  % D  "  , K );  61 Puts ( "  =====================Print the result ======================  "  );  62       For (I = 0 ; I <K; I ++ ){  63 Printf ( " % S: \ t % d \ n  "  , Words [Keys [I]. Word, words [Keys [I]. Count );  64   }  65 Puts ( "  ==================== End ==================  "  );  66       Return   0  ;  67 }

 

Source: http://www.cnblogs.com/amboyna/archive/2009/12/05/1617387.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.