Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.m
- // Exercise 6
- //
- // Created by Steve on 11/8/12.
- // Copyright (c) 2012 Personal. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- int wordCount(char * s) {
- int retval = 0;
- int isWord = 0;
- while (*s) {
- if (!isspace(*s)) {
- if (!isWord) {
- isWord = 1;
- retval++;
- }
- } else {
- isWord = 0;
- }
- s++;
- }
- return retval;
- }
- int letterCount(char * s) {
- int retval = 0;
- while (*s) {
- if (!isspace(*s++)) {
- retval++;
- }
- }
- return retval;
- }
- int main(int argc, const char * argv[])
- {
- char * testString = "This has a bunch of spaces.";
- int wc = wordCount(testString);
- int lc = letterCount(testString);
- printf("%s\n", testString);
- printf("Word count is %d.\n", wc);
- printf("Letter count is %d.\n", lc);
- printf("Average word length is %f.\n", (float)lc / (float)wc);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement