Advertisement
Garey

Way #1

Oct 22nd, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int string_length(char*);
  4. void reverse(char*);
  5.  
  6. main()
  7. {
  8.    char string[100];
  9.  
  10.    printf("Enter a string\n");
  11.    gets(string);
  12.  
  13.    reverse(string);
  14.  
  15.    printf("Reverse of entered string is \"%s\".\n", string);
  16.  
  17.    return 0;
  18. }
  19.  
  20. void reverse(char *string)
  21. {
  22.    int length, c;
  23.    char *begin, *end, temp;
  24.  
  25.    length = string_length(string);
  26.    begin  = string;
  27.    end    = string;
  28.  
  29.    for (c = 0; c < length - 1; c++)
  30.       end++;
  31.  
  32.    for (c = 0; c < length/2; c++)
  33.    {
  34.       temp   = *end;
  35.       *end   = *begin;
  36.       *begin = temp;
  37.  
  38.       begin++;
  39.       end--;
  40.    }
  41. }
  42.  
  43. int string_length(char *pointer)
  44. {
  45.    int c = 0;
  46.  
  47.    while( *(pointer + c) != '\0' )
  48.       c++;
  49.  
  50.    return c;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement