By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

reverse arrays

P: n/a
was needing to build an array that prints backwards ( stringReverse )
and returns nothing. should stop processing and print nothing when null
is encountered. this is what i have so far. was going to build my
function from the call but could not make it compile from this point.



#include <stdio.h>
#define SIZE 30

int main()
{
int loop;

char strArray[ SIZE ] = "Print this string backwards.";
for ( loop = 0; loop < SIZE; loop++ ) {
printf( "%c", strArray[ loop ] );
}

printf( "\n" );
stringReverse( strArray );
printf( "\n" );

return 0;

}

Nov 17 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
simo wrote:
was needing to build an array that prints backwards ( stringReverse )
and returns nothing.
You mean a function.
should stop processing and print nothing when null is encountered.
Encountered where?
this is what i have so far. was going to build my
function from the call but could not make it compile from this point.

#include <stdio.h>
#define SIZE 30

int main()
{
int loop;

char strArray[ SIZE ] = "Print this string backwards.";

for ( loop = 0; loop < SIZE; loop++ ) {
printf( "%c", strArray[ loop ] );
}

printf( "\n" );
stringReverse( strArray );
printf( "\n" );

return 0;
}
Okay, this is the testcode in your homework assignment, where is YOUR code?

Uli

Nov 17 '06 #2

P: n/a
simo said:
was needing to build an array that prints backwards ( stringReverse )
and returns nothing. should stop processing and print nothing when null
is encountered. this is what i have so far. was going to build my
function from the call but could not make it compile from this point.
I've modified your code somewhat. In its current state, it will compile and
run - but it won't reverse the string. All you have to do is write in the
details of stringReverse - which is a poor name choice for an abstruse
technical reason but we'll let that slide for now.
#include <stdio.h>

void stringReverse(char *s)
{
/* 1. find the length of the input - if it's
0 or 1, forget it and return. */

/* 2. get a marker (A) - a pointer or an index - to indicate
the first character in the string */

/* 3. get a marker (B) - a pointer or an index - to indicate
the last character before the null terminator */

/* 4. you'll need a temporary swap space too - a single char will do */

/* 5. for as long as marker A and marker B haven't yet collided... */

{

/* 6. copy the last unswapped-so-far value from
marker B into your temp */

/* 7. copy the first unswapped-so-far value
from marker A to marker B */

/* 8. copy the saved value from the
temp to marker A */

/* 9. add 1 to marker A */

/* 10. subtract 1 from marker B */
}
}

int main(void)
{
char strArray[] = "Print this string backwards.";

printf( "%s\n", strArray);

stringReverse( strArray );

printf( "%s\n", strArray);

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.