Advertisement
smeacham

Word Counter

Nov 8th, 2012
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. //
  2. //  main.m
  3. //  Exercise 6
  4. //
  5. //  Created by Steve on 11/8/12.
  6. //  Copyright (c) 2012 Personal. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. int wordCount(char * s) {
  12.     int retval = 0;
  13.     int isWord = 0;
  14.    
  15.     while (*s) {
  16.         if (!isspace(*s)) {
  17.             if (!isWord) {
  18.                 isWord = 1;
  19.                 retval++;
  20.             }
  21.         } else {
  22.             isWord = 0;
  23.         }
  24.         s++;
  25.     }
  26.    
  27.     return retval;
  28. }
  29.  
  30. int letterCount(char * s) {
  31.     int retval = 0;
  32.  
  33.     while (*s) {
  34.         if (!isspace(*s++)) {
  35.             retval++;
  36.         }
  37.     }
  38.    
  39.     return retval;
  40. }
  41.  
  42. int main(int argc, const char * argv[])
  43. {
  44.     char * testString = "This has      a bunch of spaces.";
  45.    
  46.     int wc = wordCount(testString);
  47.     int lc = letterCount(testString);
  48.    
  49.     printf("%s\n", testString);
  50.     printf("Word count is %d.\n", wc);
  51.     printf("Letter count is %d.\n", lc);
  52.     printf("Average word length is %f.\n", (float)lc / (float)wc);
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement