Advertisement
codemonkey

Untitled

Sep 6th, 2011
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define USAGE() printf("USAGE: abbr \"sentence\"\n")
  6.  
  7. int main(int argc, const char** args)
  8. {
  9.     if(argc == 2)
  10.     {
  11.         // Make a char array half the length of
  12.         // the input string to make sure it's long
  13.         // enough
  14.         char initials[strlen(args[1]) / 2],
  15.             chr;
  16.  
  17.         // A boolean to mark the next loop as an initial
  18.         short next = 1;
  19.  
  20.         // Variables to keep track of index
  21.         int strcount = 0, initcount = 0;
  22.  
  23.         // Browse through every char
  24.         while(chr = args[1][strcount])
  25.         {
  26.             // Increase index
  27.             strcount++;
  28.  
  29.             // If we see a space character, mark the
  30.             // next character as an initial
  31.             if(chr == ' ' && strcount > 0)
  32.             {
  33.                 next = 1;
  34.                 continue;
  35.             }
  36.  
  37.             // If marked as an initial
  38.             if(next)
  39.             {
  40.                 initials[initcount] = chr;
  41.                 next = 0;
  42.                 initcount++;
  43.             }
  44.         }
  45.  
  46.         // Nullbyte!
  47.         initials[initcount] = '\0';
  48.  
  49.         // Print out the initials
  50.         printf("%s\n", initials);
  51.     }
  52.     else USAGE();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement