Odd String Problems
Author Message
Odd String Problems

What I want to do is simple: A function takes a string and shifts it to
the right, with the lest letter being brouhgt back to the front.  So I
accomplished this by making a char * holding the last letter and strcating
the two together, BUT I can't seem to get it to stick.  It will return
fine so that if I do something  like:

printf("%s", rightshift(string));

it will do the correct output, but string will not be altered.  How can I
get around this?

--
"You've got to keep your sense of humor when you've been locked
underground" - Joss Whedon

Mon, 08 May 2000 03:00:00 GMT
Odd String Problems

Quote:

> What I want to do is simple: A function takes a string and shifts it to
> the right, with the lest letter being brouhgt back to the front. (...)

Try this:

char *shift_right_1(char *s)
{
char c,*p=s;
if (!*p) return s;  /* nothing to shift */
while(*p) p++;      /* go to the end of the string */
c=*--p;             /* the last character */
while(p>s) { *p=*(p-1); p--;}
*s=c;
return s;

Quote:
}

or this:

char *shift_right_2(char *s)
{
char c;
if (!*s) return s;
c=s[strlen(s)-1];
memmove(s+1,s,strlen(s)-1);
*s=c;

Quote:
}

Regards
Piotr

Mon, 08 May 2000 03:00:00 GMT
Odd String Problems

Quote:

>What I want to do is simple: A function takes a string and shifts it to
>the right, with the lest letter being brouhgt back to the front.  So I
>accomplished this by making a char * holding the last letter and strcating
>the two together, BUT I can't seem to get it to stick.  It will return
>fine so that if I do something  like:

>printf("%s", rightshift(string));

>it will do the correct output, but string will not be altered.  How can I
>get around this?

Try:

char* rightshift(char* string)
{
int len = strlen(string);
if (len > 1)    /* sanity check -- we obviously need to do no  */
/* work for an empty or single character string */
{
/* Save the last character */
char last = string[len-1];
int n;

/* Perform the shift right */
for (n = len-1; n > 0; n--)
string[n] = string[n - 1];

/* Now put that original, last character in the front */
string[0] = last;
}

return string;

Quote:
}

-- Tom

/==================================================================\
Thomas W. Brown                         Director of Development

Phone: (619)676-7854               10875 Rancho Bernardo Rd., #200
Fax  : (619)676-7710                      San Diego, CA  92127
\==================================================================/

Tue, 09 May 2000 03:00:00 GMT
Odd String Problems

...

Quote:
>Try:

>char* rightshift(char* string)
>{
>    int len = strlen(string);
>    if (len > 1)    /* sanity check -- we obviously need to do no  */
>                         /* work for an empty or single character string */
>    {
>        /* Save the last character */
>        char last = string[len-1];
>        int n;

>        /* Perform the shift right */
>        for (n = len-1; n > 0; n--)
>            string[n] = string[n - 1];

>        /* Now put that original, last character in the front */
>        string[0] = last;
>    }

>    return string;
>}

Consider using memmove() here which, if the impelementor has done his
homework, should be a fast way of copying where source and destination
overlap:

char* rightshift(char* string)
{
size_t len = strlen(string);

if (len > 1) {
char last = string[len-1];

memmove(string+1, string, len-1);

string[0] = last;
}

return string;

Quote:
}

--
-----------------------------------------

-----------------------------------------

Sun, 14 May 2000 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages