Moortiii icon

Untitled

Moortiii | PRO | 09/27/17 07:02:34 PM UTC | 0 ⭐ | 396 👁️ | Never ⏰ | []
C |

1.18 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <string.h>
 
void reverseString(char original_array[], char reversed_array[]);
 
int main() {
    char user_string[255] = {0}; // Empty the arrays to make sure no random pieces of memory are left
    char reverse_string[255] = {0}; // Empty the arrays to make sure no random pieces of memory are left
 
    printf("Please enter a word:\n\n");
    fgets(user_string, sizeof(user_string), stdin); // Arguments: 1) destination 2) character limit 3) from keyboard
    reverseString(user_string, reverse_string); // Run our newly made function
 
    return 0;
}
 
void reverseString(char original_array[], char reversed_array[]) { // Pass a source and destination array as args
    int index = 0; // This will be used to
    int str_len = strlen(original_array); // Get the string length of the original array
 
    for(int i = str_len - 1; i >= 0; i--) { // Loop over the original array backwards
        reversed_array[index] = original_array[i]; // Set the index of reversed_array to the current char in the loop
        index++; // Increment index to make Index and I grow towards each other
    }
 
    printf("The array reversed is: %s", reversed_array);
}

Comments