I am currently studying c programming, but I'm at a very basic level. So for practice I made a program that reads a string and inverts it. I want to know if the code is readable and how can I can improve it.
/*
* Goal: Given a string, invert it, then print the result.
* Author: Lúcio Cardoso
*/
#include <stdio.h>
#include <string.h>
/*This is the size of the arrays in this program. It's better to use a
constant, because makes easier to modify the arrays' size, if needed.*/
#define NUM 200
void invert(char *s) //Fucntion responsible for inverting the string.
{
char aux[NUM];
int i, j, n; //These variables are all used to control the for loops.
/*This loop will read the size of the array's string, that's why
strlen(s) - 1 is used. We don't need to read all contents
from the array, just the string. As we read the s[] backwards,
its content is stored in aux[n]. In this case, from zero to the
size of s[]'s string - 1. Minus 1 is used because we read it from ZERO,
not i. */
for (i = strlen(s) - 1, n = 0; n < strlen(s), i >= 0; i--, n++) {
aux[n] = s[i];
}
printf("Included with the super mojo from the string inverter, ");
printf("this is the result: ");
//This for loop reads all the content from the aux array, and print it.
for (j = 0; j < strlen(s); j++) {
printf("%c", aux[j]);
}
}
int main(void)
{
char string[NUM];
printf("\nWelcome to the super, duper string inverter!\n");
printf("If you want to see some magic, enter a string:\n>");
gets(string); //Reads the string.
//Now, we only need to call the function with string[] as a parameter.
invert(string);
return 0;
}