SDDSlib
Loading...
Searching...
No Matches
strslide.c File Reference

Implements the strslide function. More...

#include "mdb.h"

Go to the source code of this file.

Functions

char * strslide (char *s, long distance)
 Slides character data within a string by a specified distance.
 

Detailed Description

Implements the strslide function.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file strslide.c.

Function Documentation

◆ strslide()

char * strslide ( char * s,
long distance )

Slides character data within a string by a specified distance.

This function shifts the characters in the string s by the given distance. A positive distance slides characters toward higher indices (to the right), while a negative distance slides characters toward lower indices (to the left).

Parameters
sPointer to the null-terminated string to be modified.
distanceThe number of positions to slide the string. Positive values shift characters to the right, and negative values shift characters to the left.
Returns
Returns the modified string s on success. Returns NULL if the distance is greater than the string length when sliding to higher indices.

Definition at line 32 of file strslide.c.

32 {
33 char *source, *target;
34 long i, length;
35
36 if (!s || !distance)
37 return s;
38 if (distance > 0) {
39 /* slide toward higher index */
40 source = s + (length = strlen(s));
41 if (distance > length)
42 return NULL;
43 target = source + distance;
44 for (i = length; i >= 0; i--)
45 *target-- = *source--;
46 } else if (distance < 0) {
47 /* slide toward lower index */
48 length = strlen(s);
49 if ((distance = -distance) >= length)
50 *s = 0;
51 else {
52 source = s + distance;
53 target = s;
54 do {
55 *target++ = *source++;
56 } while (*source);
57 *target = 0;
58 }
59 }
60 return s;
61}