Advertisement
smeacham

removeExtraSpaces()

Nov 8th, 2012
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. //
  2. //  main.m
  3. //  Exercise V
  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. char * removeExtraSpaces(char * s) {
  12.     char * retval = malloc(strlen(s) + 1);
  13.     char * out = retval;
  14.    
  15.     int isSpaces = 0; // Indicates whether we're currently copying/reducing a series of one or more spaces.
  16.    
  17.     while (*s) {
  18.         if (*s != ' ' || !isSpaces) {
  19.             isSpaces = (*s == ' ');
  20.             *out++ = *s;
  21.         }
  22.         s++;
  23.     }
  24.     *out = 0;
  25.    
  26.     return retval;
  27. }
  28.  
  29. int main(int argc, const char * argv[])
  30. {
  31.     char * testString = "This has      a bunch of spaces.";
  32.    
  33.     printf("%s\n", testString);
  34.     printf("%s\n", removeExtraSpaces(testString));
  35.    
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement