Advertisement
Narayan

string code

Mar 4th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. /*-----------main.c-----------*/
  2. #include <stdio.h>
  3. #include "text.h"
  4. #include "mem.h"
  5.  
  6. int main (int argc, char** argv){
  7.     char* string = NULL;
  8.  
  9.     string = mem_alloc (BUFFER * sizeof (char));
  10.     fgets (string, BUFFER, stdin);
  11.     if (string [strlen (string) - 1] == '\n')
  12.       string [strlen (string) - 1] = '\0';
  13.     printf ("The string is: %s\n", string);
  14.     string = invert_string (string);
  15.     printf ("The inverted string is: %s\n", string);
  16.     free (string);
  17.     return 0;
  18. }
  19.  
  20. /*-----------text.c-----------*/
  21. #include <stdio.h>
  22. #include "text.h"
  23. #include "mem.h"
  24.  
  25. /*****Invert String*****/
  26. char* invert_string (char* string){
  27.   char *tmp_str = NULL;
  28.   short int len = 0, i = 0;
  29.  
  30.   len = strlen (string);
  31.   tmp_str = mem_alloc (BUFFER * sizeof (char));
  32.  
  33.   for (i = len; i >= 0; i--)
  34.     tmp_str [len - i] = string [i - 1];
  35.  
  36.   tmp_str [len + 1] = '\0';
  37.   string = tmp_str;
  38.   //free (tmp_str);
  39.   return tmp_str;
  40. }
  41.  
  42. /*-----------text.h-----------*/
  43. #pragma once
  44.  
  45. #include <string.h>
  46.  
  47. #define BUFFER 256
  48.  
  49. char* invert_string (char* string);
  50.  
  51. /*-----------mem.c-----------*/
  52. #include "mem.h"
  53.  
  54. /*****Memory Control*****/
  55. int mem_error = MEM_ERROR_OK;
  56.  
  57. void* mem_alloc (const size_t size){
  58.     void* result;
  59.     if ((result = malloc(size)) == NULL){
  60.         mem_error = MEM_ERROR_NO_MEMORY;
  61.     }
  62.     return result;
  63. }
  64.  
  65. int mem_get_error(void){
  66.     return mem_error;
  67. }
  68.  
  69. int mem_clear_error(void){
  70.     return mem_error = MEM_ERROR_OK;
  71. }
  72.  
  73. /*-----------mem.h-----------*/
  74. #pragma once
  75.  
  76. #include <stdlib.h>
  77.  
  78. #define MEM_ERROR_OK             0
  79. #define MEM_ERROR_NO_MEMORY     -1
  80.  
  81. void* mem_alloc(const size_t size);
  82.  
  83. int   mem_get_error(void);
  84. int   mem_clear_error(void);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement