The error is here:
while (*sPtr != '\0') {
++length;
++sPtr;
}
At this point the sPtr point at the end of the string so in the second loop it never decrement.
for (int i = length; i >= 0; --i) {
printf("%c", *(sPtr+1));
}
A possible solution can be this:
for (int i = length; i >= 0; --i) {
printf("%c", *(sPtr));
--sPtr;
}
Answer from Zig Razor on Stack OverflowSo i just got to pointers in the K&R C programming book and one of the challenges is to rewrite the functions we worked on previously and implement pointers. i am trying to understand the topics as well as i can before moving forward in the book so if you guys could tell me the best practices and what i should have done in this snippet of code i would greatly appreciated. for reference i was thinking about how i see temp numbers like i used less and less in replacement of ( ; check ; increment ). sorry if this post seems amateur.
#include <stdio.h>
#include <string.h>
void reverse(char *s) {
char temp[20];
int len = strlen(s);
s += len - 1;
int i = 0;
while (len--) {
temp[i++] = *s--;
}
temp[i] = '\0'; // Null-terminate the reversed string
printf("%s\n", temp); // Print the reversed string
}
int main(void) {
char string[20] = "hello world";
reverse(string);
return 0;
}
#include <stdio.h>
#include <string.h>
void reverse(char *s) {
char temp[20];
int len = strlen(s);
s += len - 1;
int i = 0;
while (len--) {
temp[i++] = *s--;
}
temp[i] = '\0'; // Null-terminate the reversed string
printf("%s\n", temp); // Print the reversed string
}
int main(void) {
char string[20] = "hello world";
reverse(string);
return 0;
}Videos
The error is here:
while (*sPtr != '\0') {
++length;
++sPtr;
}
At this point the sPtr point at the end of the string so in the second loop it never decrement.
for (int i = length; i >= 0; --i) {
printf("%c", *(sPtr+1));
}
A possible solution can be this:
for (int i = length; i >= 0; --i) {
printf("%c", *(sPtr));
--sPtr;
}
This is the solution I came up with. Thanks for the help anyone provided!
while (*sPtr != '\0') {
++length;
++sPtr;
}
printf("Reverse string = ");
sPtr = sentence;
for (int i = length; i >= 0; --i) {
printf("%c", *(sPtr+length));
--sPtr;
}
Your compilation error is due to the fact that you call reverse2 in main, but the C compiler doesn't know inside of main what reverse2 is yet because reverse2 is defined after main. (The C compiler must be able to compile a .c file in a streaming fashion, so this means that everything it needs to compile something - like a function - must have been defined beforehand)
If you invert the order of the two functions (i.e. you put reverse2 before main), or add this declaration:
void reverse2(char*, char*);
before main() then that error won't happen.
(I haven't checked your code, so I don't know if it does what you want it to do, but your question is about the compilation error: not whether the code actually inverts the string)
1 - prog.c:8:2: warning: implicit declaration of function 'reverse2' is invalid in C99 [-Wimplicit-function-declaration] reverse2(forward, back);
This error is because you need to define reverse2() before main tries to call it, simply move your code for reverse2() prior to the code for main().
2 - you need to null terminate your backwards string in reverse2()
for (i = 0; i <= counter; i++) {
*string2 = *string1;
string2 += 1;
string1--;
}
*string2 = '\0';
3 - it does no good trying to print out string2 in reverse2() because it points to the end of the string. Print back in main() and you will see the expected results.
reverse2(forward, back);
printf("%s", back);