Advertisement
Guest User

sort_char.c

a guest
Sep 30th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. /*
  2.   [sort_char.c]
  3. Author : AlexZ ( alessandro.suglia@gmail.com )
  4. Date : 30/09/2011 07:57 PM  
  5.  
  6. Simple program which takes an undefined
  7. length string and sort alphabetically each
  8. of its char.
  9.  
  10. */
  11.  
  12. /* <------------Header Functions Declaration -----------------> */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18.  
  19. /* <-----------Functions Declarations -----------------------> */
  20.  
  21. char *sort_char( const char * );
  22. int count_argv( char *argv[], int argc );
  23. char *strcat_argv( char *s, int len, char *argv[], int argc );
  24.  
  25. /* <------------- main.c ------------------------------------> */
  26. int main(int argc, char *argv[] )
  27. {
  28.   char *s = NULL;
  29.   int len = 0; /* variable which holds the entire length of the argument passed through the command line*/
  30.  
  31.   if ( argc < 2 ) /* Too few arguments, something was wrong */
  32.     {
  33.       perror("<<<<USAGE<<<<< : ./ord_char 'word'\nTry again passing a string\n");
  34.       exit(-1);
  35.      
  36.     }
  37.  
  38.   len = count_argv(argv, argc); /* call to the function count_argv */
  39.   s = strcat_argv(s, len, argv, argc); /* call to the function strcat_argv */
  40.  
  41.   if ( !(s) ) /* Memory Allocation test */
  42.     {
  43.       perror("Malloc error : FATAL ERROR!\n");
  44.       exit(-1);
  45.     }
  46.   s = sort_char(s); /* Call ord_char function */
  47.  
  48.   printf("SORTED STRING %s : %s\n", argv[1],s);
  49.  
  50.   return EXIT_SUCCESS;
  51.      
  52. }
  53. /* Count the number of argument that were passed through the command line */
  54. int count_argv( char *argv[], int argc)
  55. {
  56.   int i;
  57.   int len = 0;
  58.  
  59.   for ( i = 1; i < argc; i++ )
  60.     len += strlen(argv[i]);
  61.  
  62.   return len;
  63.  
  64. }
  65. /* Concatenates the command line arguments in a single string */
  66. char *strcat_argv( char *s, int len, char *argv[], int argc )
  67. {
  68.   int i;
  69.  
  70.   s = (char*)malloc(len * sizeof(char));
  71.   if ( !(s) )
  72.     {
  73.       perror("Malloc error : FATAL ERROR!\n");
  74.       exit(-1);
  75.     }
  76.   for ( i = 1; i < argc; i++ )
  77.     strcat(s, argv[i]);
  78.  
  79.   return s;
  80. }
  81. /* Sorts alphabetically each string's char */
  82. char *sort_char( const char *s )
  83. {
  84.   int i;
  85.   int high = strlen(s);
  86.   char *str;
  87.   char c;
  88.  
  89.   str = (char*)malloc(high * sizeof(char));
  90.   if ( !(str) )
  91.     {
  92.       perror("MALLOC ERROR : Fatal!\n");
  93.       exit(-1);
  94.     }
  95.   strcpy(str, s);
  96.   /* Simple bubble sort implementation */
  97.   while ( high > 1 )
  98.     {
  99.       for ( i = 0; i < high-1; i++ )
  100.     if ( str[i] > str[i+1] )
  101.       {
  102.         c = str[i];
  103.         str[i] = str[i+1];
  104.         str[i+1] = c;
  105.  
  106.       }
  107.  
  108.       high--;
  109.  
  110.  
  111.     }
  112.  
  113.   return str;
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement