Moortiii

Untitled

Sep 27th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void reverseString(char original_array[], char reversed_array[]);
  5.  
  6. int main() {
  7.     char user_string[255] = {0}; // Empty the arrays to make sure no random pieces of memory are left
  8.     char reverse_string[255] = {0}; // Empty the arrays to make sure no random pieces of memory are left
  9.  
  10.     printf("Please enter a word:\n\n");
  11.     fgets(user_string, sizeof(user_string), stdin); // Arguments: 1) destination 2) character limit 3) from keyboard
  12.     reverseString(user_string, reverse_string); // Run our newly made function
  13.  
  14.     return 0;
  15. }
  16.  
  17. void reverseString(char original_array[], char reversed_array[]) { // Pass a source and destination array as args
  18.     int index = 0; // This will be used to
  19.     int str_len = strlen(original_array); // Get the string length of the original array
  20.  
  21.     for(int i = str_len - 1; i >= 0; i--) { // Loop over the original array backwards
  22.         reversed_array[index] = original_array[i]; // Set the index of reversed_array to the current char in the loop
  23.         index++; // Increment index to make Index and I grow towards each other
  24.     }
  25.  
  26.     printf("The array reversed is: %s", reversed_array);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment