Posted by Anonymous Sat 17th Mar 2007 10:40 - Syntax is C - 40 views
Download | New Post | Modify | Hide line numbers
  1. #include
  2. #include
  3.  
  4. unsigned int string_length( char * string )
  5. {
  6.     unsigned int string_length;
  7.     for ( string_length = 0; string[string_length] != '\0'; string_length++ )
  8.     {
  9.         string_length = string_length + 1;
  10.     }
  11.     return ( string_length - 1 );
  12. }
  13.  
  14. char * reverse_string( char * string )
  15. {
  16.     int length = string_length( string );
  17.     char * reversed_string = malloc( length + 1 );
  18.     if ( reversed_string != NULL )
  19.     {
  20.         int each = 0, each_reversed = length - 1;
  21.         while ( each_reversed >= 0 )
  22.         {
  23.             reversed_string[each] = string[each_reversed];
  24.             each++; each_reversed--;
  25.         }
  26.         return reversed_string;
  27.     }
  28. }
  29.  
  30. char * squeese( char * string )
  31. {
  32.     char * squeesed_string = malloc( sizeof( string ) + 1 );
  33.     int each, character;
  34.     for ( character = 0, each = 0; string[character] != '\0'; character++, each++ )
  35.     {
  36.         if ( string[character] == string[character + 1] )
  37.         {
  38.             each--;
  39.             continue;
  40.         }
  41.         squeesed_string[each] = string[character];
  42.     }
  43.     return squeesed_string;
  44. }
  45.  
  46. int main( )
  47. {
  48.     char string[] = "hammad";
  49.     printf( "String: %s\n", string );
  50.     printf( "Reversed: %s\n", reverse_string( string ) );
  51.     printf( "Squeesed: %s\n", squeese( string ) );
  52. }

PermaLink to this entry https://pastebin.co.uk/11950
Posted by Anonymous Sat 17th Mar 2007 10:40 - Syntax is C - 40 views
Download | New Post | Modify | Hide line numbers

 

Comments: 0