I am trying to reverse a character string in C
Here is what I have
void reverse(char str[]) {
int i = 0;
int length;
// Get string length
for (i = 0; str[i] != '\0' ; ++i) {
length = i;
}
char reversed[1000];
int j;
j = 0;
// Reverse it
for (j = 0; j < length ; ++j) {
reversed[j] = str[length - j];
}
}
I know that reversed contains the string reversed, but I’m not sure how to modify the original str without throwing away data I need.
I also don’t know how to set strto reversed without looping again.
Would it be acceptable to do another…
int m;
m = 0;
for (m = 0; m < length ; ++m) {
str[j] = reversed[j];
}
Usually I’d say this many loops smells, but I’m also still quite unfamiliar with the language so I’m not sure…
Update
Thanks for all the answers guys, and I appreciate the edits too!
I ended up going with this…
int main() {
char str[] = "Reverse me!";
int length;
for (length = 0; str[length] != '\0'; length++) {
}
printf("length => %d chars\n", length);
int j, k;
char c;
for (j = 0, k = length - 1; j < k; j++, k--) {
c = str[k];
str[k] = str[j];
str[j] = c;
}
printf("reversed => %s\n", str);
return 0;
}
Some things I now know…
- There is a
strlen()like in PHP. However, it has not been discussed in the book yet, plus I need to get familiar with null terminated strings. - A
forloop can assign and do multiple things that are comma separated. I never knew this!
So asking was worthwhile 🙂
Comments on your code:
Rather than copying the i to length every time you could just wait until the end.
Though the compiler would probably be able to make this optimization for you.
You should know, though, that there is a standard string function
strlen(#include <string.h>)which will measure the length of a char string using the same general algorithm (look for the end) but is usually optimized for the target processor.Your code again:
Using big arrays are good for learning and simple examples, but you can also allocate memory dynamically based on the size you now know you need. The standard function for doing this is
mallocwhich is instdlib.h(also inmalloc.h). Memory allocated with this function should also be freed, though.There are also other functions in the malloc family. There’s
calloc, which allocates memory and clears sets it to 0. There is also a function calledstrdup(which isn’t in standard C, but is very widely available in string.h) which takes a string and allocates a duplicate of it. It is really just:Another useful memory allocation function is
alloca(not in the C standard, but widely available and similar functionality is available with variable length arrays in C99). It is great, but works differently frommalloc. It allocates memory that is only useable until the current function returns because this memory is allocated in the same way as memory for local variables (from the stack).More of your code:
The code:
should swap the order of the string without making a copy of it. For strings with odd length it won’t try to swap the middle char with itself.