-
strings in reverse order
would you say these loops are correct? if not could anyone tell me why or give me any hints
the question is.... Write two loops to display the characters of the string s in reverse order. One should use array notation, and one pointer notation.
array
Code:
char s[100];
int i;
for( i=99; i==0; i--)
printf(“%c”, s[i]);
pointer
Code:
char *sp;
sp=&s[99];
do { printf("%c", *sp);
sp--; }
while (sp != s);
-
>> would you say these loops are correct?
What output do you get? Does it look correct to you?
-
The first one is wrong with only a first glance. Evaluate it step by step. What is the mistake?
The second one is almost correct. Tip: You know that in C strings can be shorter than the array's size. What terminates a string is the null character '\0'.
-
How about...
Code:
int O_0( char *O_O )
{
if( (O_O) && *(O_O) )
{
(O_0)( O_O + 1 );
putchar( *(O_O) );
}
return (0x0);
}
Quzah.
-
Check your for loop
Code:
for( i=99; i==0; i--)
What does it say, start the index at 99 and cheap decrementing the index only if index is 0. Does that make any sense? NO
What you need is index should be decrementing until i >= 0 . Make sense ???
-ssharish
-
Code:
void revprint(const char *s)
{
const char *end = s;
for(;*end;++end);
while(--end >= s)
putchar(*end);
}