There are a number of ways to approach this problem, but I like doing it in one of two (maybe archaic but simple) ways. You did well using just arrays but I'll explain a good way to approach this using beginners C level functions.
One way to invert a string is to run a for
loop (or while
loop) just like you did. However, you should familiarize yourself with pointers so you don't have to run your second for
loop to print out all of the characters individually. You're already implementing some pointer logic with your arrays, but I'll add some information that should be helpful.
Character Pointers and Strings
Say I want a string that holds 100 characters, but I want to make use of pointers instead of starting with an array. Simply create a char*
and assign it a 100 slots of memory with malloc()
(+1 slot for the null character which terminates the string). malloc
stands for memory allocate.
char* string;
string = (char*)malloc(101*sizeof(char));
//char string[101] is the same as this code above
Here string holds 101 places in memory that are size char
(8 bits). String can be used just like an array to look at any one character. If we now store a bunch of information in this area with fgets()
or scanf()
, we can access any one character just like you did with aux[j]
.
for (j = 0; j < strlen(s); j++) {
printf("%c", aux[j]);
}
Printing an Entire String
However, if we want to print the entire string after assigning the values/characters, simply use %s
instead of looping through the entire array for each character. Let's use my predefined string pointer.
printf("%s", string);
Here you need to make sure your string ends with a null character '\0'
or this printf
will print out unassigned space in memory converted to character form.
Inverting a String
Do as you just did, by assigning the last position of your initial string (s) to the first position of your inverted string (aux) and continue on throughout the array. You did this well.
Freeing Pointer Memory
Finally, don't forget to free the memory you used and don't need anymore when moving on in your programs. For this simple project, freeing the memory you allocated for the pointer shouldn't matter all that much but is good for you to practice. This works only for things you've used malloc()
with. This will free up the space you reserved (once you free, you're not getting that data back).
free(string);
Simple pointer code:
char* string;
string = (char*)malloc(101*sizeof(char));
//creates a pointer and gives it 101 slots of memory size character
fgets(string,100,stdin);
//retrieves input of 100 through stdin (your input) and stores in string
printf("You entered: %s\n", string);
//prints out what you stored in string
printf("A single character: %c\n", string[5]);
//prints out what you stored in position 5 of string
//assuming your string made it to at least 5
free(string);
//clears out the memory locations you reserved for string
Further Pointer Information
Pointers are confusing for most to learn about at first. They just point to information, they don't know what it is or care about what happens to it. Think of a pointer as a guy whose job is to point at your wallet so you always know where it's at. The guy doesn't care about how much money is inside, he just tells you the location of the wallet whenever you ask him. De-referencing his information (going to the location yourself and looking at the value) will tell you how much money is inside your wallet.
You can have two pointers pointing to the same thing, or one pointer pointing at another pointer. A pointer can direct you toward 1 thing or have it point at many things by using malloc()
. You can reuse the same char*
after freeing it, if you plan to point to more than one thing you need to malloc()
however many spaces. A pointer can be an int
, char
, float
, etc. all predefined with a *
.
int* a;
float* b;
char* c;
dog* d; //after you "typedef dog"
char**e; //used for creating a list of pointers or 2D arrays
int f[300][300]; //same as char** e but of type int;
//etc
//if you want to use a string for later use after freeing it, point to NULL
c = NULL;
When I was learning to better implement pointers, I watched a video by suckerpinch about adding words to one another and making a Portmanteau. If you want to better familiarize yourself with pointers, try making a simple version of this where you concatenate strings (adding strings to one another - possibly helpful function: strcat()
), print strings out frequently, etc. If you can make something like this, you'll get a fairly good handle on strings/pointers.
gets()
as it will allows the user to overrun the input buffer. It has been depreciated for a while and completely removed in the latest C standard. Suggest usingfgets( string, sizeof(string), stdin );
\$\endgroup\$strlen()
actually returns asize_t
value, not aint
value. To avoid theimplicit conversions, use
size_t` \$\endgroup\$