9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia05/ex20.c (558B)
   1 #include <stdio.h>
   2 #include <string.h>
   3 
   4 typedef enum {
   5     OK,
   6     ERROR_NULL_POINTER
   7 } status_t;
   8 
   9 status_t string_reverse(char *);
  10 void swap_char(char *a, char *b);
  11 
  12 int main (void)
  13 {
  14     char str[] = "Hello world!";
  15 
  16     puts(str);
  17     string_reverse(str);
  18     puts(str);
  19 
  20     return 0;
  21 }
  22 
  23 status_t string_reverse(char *str)
  24 {
  25     if(str == NULL) return ERROR_NULL_POINTER;
  26 
  27     size_t len = strlen(str);
  28     for(size_t i = 0; i < (len / 2); i++)
  29         swap_char(&str[i], &str[len - 1 - i]);
  30 
  31     return OK;
  32 }
  33 
  34 void swap_char(char *a, char *b)
  35 {
  36     *a = *a + *b;
  37     *b = *a - *b;
  38     *a = *a - *b;
  39 }