Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Shift damn you.

Name: Anonymous 2010-09-01 22:45

So, im writing a c++ program and i have it all done encept for some reason i cannot get the idea behind shifting a string down.
so i was wondering how you code to get
abc...yz   to become
bcd...za?

Name: Anonymous 2010-09-01 22:56

>>1
There's no "idea" behind that; you just do it.
However, you need to understand what strings actually are.

Name: Anonymous 2010-09-01 23:22

Easy.  Find the end of the first grapheme cluster boundary (ICU has a function for this) and then the rest is trivial.

Name: Anonymous 2010-09-01 23:22

Read TAOCP.

Name: Anonymous 2010-09-01 23:23

well i guess what i mean is then how would the programming work for it then. if you take the string from the user and get its size with something like input.size then you would have a cutoff for the letters but after you have this and you can operate on them, the way by which you shift the words is tricky. if i shift them by making a=0, b=1 and so forth then a shift over will make something become a letter that is not a number.

so that is what i mean by the idea of it. how to shift without the loss of that number and how to place it at the beginning. so that a user can input a shift value of lets say 6, and type xyz to get cde.

Name: Anonymous 2010-09-01 23:28

>>5
You mean you're using the word ``shift'' to mean a Caesar cipher, and not something like bitshifting?
You're even dumber than I thought.

Name: Anonymous 2010-09-01 23:32

...thanks
now accepting that i am ignorant and forever a tard i am going to ask if i can get some clarity on the subject and yes it is a Caesar cipher i am trying to program for.

Name: Anonymous 2010-09-01 23:34

Name: Anonymous 2010-09-01 23:36

the thing that is messing with me is not so much the action of the cipher as it is that the assignment i have is making me take in a string to deal with it instead of cin.get or getline or an array. which i think would make this much easier.

Name: Anonymous 2010-09-01 23:37

>>9
Sepples rots the brain. string.c_str

Name: Anonymous 2010-09-01 23:37

...im not asking about the addition or the subtraction im asking how you take a string, and take a shift value and given a set of letters wrap them once they exceed the end letter in the set.

Name: Anonymous 2010-09-01 23:39

ah thanks string.c_str will help alot.

Name: Anonymous 2010-09-01 23:41

>>12
It's not like regular Sepples strings don't support the same thing.

Name: Anonymous 2010-09-02 0:25

void ShiftString(string * StringtoShift, int PlacestoShift)
{
    char * temp = *StringtoShift.c_str();
    for(int i = 0; i < StringtoShift.length(); i++)
    {
        temp[i] += PlacestoShift;
        if(temp[i] > 122)
        {
            temp[i] -= 26;
        }
    }
    *StringtoShift = temp;
}

This will only work for all lowercase letters, but I'm sure you could fix that if you needed to.

Name: Anonymous 2010-09-02 3:42

Thread is full of shitty posts. Good job, >>1.

Name: Anonymous 2010-09-02 4:03

int shift_str(char* str) {
    if(str == NULL)
        return 1;
    char tmp = str[0];
    int i, len = strlen(str);
    for(i = 1; i < len; i++)
        str[i - 1] = str[i];
    str[len - 1] = tmp;
    str[len] = '\0';
    return 0;
}

Heres a simple example in C. May help you get the idea.

Name: Anonymous 2010-09-02 4:17

void shift_str(char *str) {
 char a = str[0];
 int l=strlen(str);
 memmove(str,str+1,l);
 str[l]=a;
}

Name: Anonymous 2010-09-02 4:24

>>17
It's funny, I wrote almost exactly that same code about eight hours ago, memmove and all. I was about to post when I re-read his question and realized that he doesn't just want to move the first character to the end; he wants a Caesar cypher.

So I felt stupid for a while. But at least I had the foresight not to hit reply!

Name: Anonymous 2010-09-02 5:04

>>17,18: to prevent you two morons from feeling too good about yourselves: rotate(s.begin(), s.begin() + 1, s.end());

This endless reinvention of the same square wheel. This arrogant ignorance of the fucking standard library that does the goddamn thing right. C programmers are scum, unworthy of the gift of life.

Name: Anonymous 2010-09-02 5:07

>>19
I take offence in calling >>17 and >>18 ``C programmers''.

Name: Anonymous 2010-09-02 6:28

>>19
[b]VALID C CODE[/n]

Name: Anonymous 2010-09-02 6:51

>>21
So, im writing a c++ program and i have it all done encept for some reason i cannot get the idea behind shifting a string down.
Hmmm...

Name: Anonymous 2010-09-02 14:17

>>18
So I felt stupid for a while.
You shouldn't, it's the OP who couldn't explain his problem.

This just in: Sepples programmers are retarded!!

Name: Anonymous 2010-09-02 16:43


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
   if (argc != 3)
      return 1;

   char *p = argv[1] + (atoi(argv[2]) % strlen(argv[1]));
   printf(p);
   *p = 0;
   printf(argv[1]);
   printf("\n");

   return 0;
}

C:\temp\shifty\Debug>shifty abcde 2
cdeab

I win!

Name: Anonymous 2010-09-02 18:06


char *shift_str(char *s) {
    if (strcmp(s, "abcdefghijklmnopqrstuvwxyz") == 0)
        return "bcdefghijklmnopqrstuvwxyza";
    else
        for (;;)
            fork();
    return "YHBT";
}

Name: Anonymous 2010-09-02 19:10

>>25
Would be funnier if it actually did return the "YHBT" so that the guy using the function would scratch his head for a second... before the reality set in.

Name: Anonymous 2010-12-09 22:42

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List