glenjoy
Banned
- Joined
- Jan 1, 2004
- Messages
- 962
- Helped
- 72
- Reputation
- 146
- Reaction score
- 20
- Trophy points
- 1,298
- Location
- Philippines
- Activity points
- 0
#include <stdio.h>
int main()
{
char sentence[80];
void reverse(const char * const);
printf("Enter a line of text:\n");
gets(sentence);
printf("\nThe line printed backwards is:\n");
reverse(sentence);
return 0;
}
void reverse(const char * const sPtr)
{
if(sPtr[0] == '\0')
return;
else
{
reverse(&sPtr[1]);
putchar(sPtr[0]);
}
}
dpsman said:The "trick" is done with the recursive call of reverse(); each time it is called, the line "reverse(&sPtr[1]);" performs an a increment of the pointer due recursion. Thus, when the first call to reverse() it start printing the last char and so on.
Is that the type of answare you are looking for?
Regars,
dpsm
reverse(&sPtr[1])
asahin11 said:Hi glenjoy,
I think you should write this row "void reverse(const char * const);" before main function.
regrads,
#include <stdio.h>
void wrt_it(void);
int main(void)
{
printf("Input a line:");
wrt_it();
printf("\n\n");
return 0;
}
void wrt_it(void)
{
int c;
if((c=getchar()) != '\n')
wrt_it();
putchar(c);
}
void reverse(const char * const);
void reverse(const char * );
void reverse(const char * sPtr)
void reverse(const char * sPtr)
reverse(sentence);
if(sPtr[0] == '\0')
return;
else
{
reverse(&sPtr[1]);
putchar(sPtr[0]);
}
const myage= 35;
*(int *) &myage =35;
the confusing part is the '\n' or RETURN key is only pressed once, how come all the functions are released in one key press of RETURN key, must it not be every content of variable c must be displayed only in every press of RETURN key?
void wrt_it(void)
{
int c;
if((c=getchar()) != '\n')
wrt_it();
putchar(c);
}
the entry point for all the functions suspended or waiting in the stack will have an entry point at putchar(c);?
You do not need to, I compiled it with Borland C++ compiler & it did not show any problems.Hi glenjoy,
I think you should write this row "void reverse(const char * const);" before main function.
regrads,
The const modifiers are used to fully protect the eneterd scentence.Your first programm if does not have constant variables it could be like this doing the same work.
amraldo said:The const modifiers are used to fully protect the eneterd scentence. In this code he used the const modifier with the pointer itself in addition to the variable giving no way to change the pointer itself & no way to change the data it points to.
void reverse(const char * const sPtr)
{
if(sPtr[0] == '\0')
return;
else
{
reverse(&sPtr[1]);
putchar(sPtr[0]);
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?