Δημοσιεύτηκε: 16 Φεβ 2012, 13:23
Ιδού η εν λόγω ρουτίνα, με σχόλια για να βοηθηθείς περισσότερο...
- Κώδικας: Επιλογή όλων
/*********************************************************//**
*
************************************************************/
int s_swap_static3( char *s1, char *s2 )
{
char c = '\0' /* for swapping characters */
char *cp = NULL; /* for saving last pos of shortest string*/
int i = 0; /* for parsing the 2 strings */
/* sanity checks */
if ( !s1 || !s2 )
return 0; /* FALSE */
/* swap chars between the 2 strings, until the end of the shortest one */
for (i=0; s1[i] && s2[i]; i++)
{
c = s1[i];
s1[i] = s2[i];
s2[i] = c;
}
/* s1 has more characters (copy them to s2) */
if ( '\0' != *(cp = &s1[i]) ) { /* cp remembers in s1 in which pos s2 ended*/
while ( s1[i] )
s2[i] = s1[i++];
*cp = '\0'; /* nil-terminate s1 at cp */
}
/* s2 has more characters (copy them to s1) */
else if ( '\0' != *(cp = &s2[i]) ) {
while ( s2[i] )
s1[i] = s2[i++];
*cp = '\0';
}
/* else both strings had equal current length */
return 1; /* TRUE */
}