#A way to shift Strings to the left or right (C)

65 messages · Page 1 of 1 (latest)

grim igloo
#

I tried to make a strShift(char*, signed short) function, but it seems to not work and break everything. What's wrong with it or what's the right way to do it. 🥹

woeful phoenix
#

Would you mind posting your code and not just a picture?

grim igloo
#

Lol

#
void strShift(char* string, signed short N) {
    short Len = strLen(string);
    
    if( N ==0 ) return;
    if( N < 0 ) {
        for(int i = 0; i < Len -N; i++) {
            string[i] = string[i+N];
        }
        return;
    }
    if( N > 0 ) {
        for(int i = Len; i >= 0 +N; i--) {
            string[i] = string[i-N];
        }
        return;
    }
}```
woeful phoenix
#

thank you

#

So what's the goal here?

#

If you have the string "Hello world!" and want to shift it 5 to the right, what do you want to happen?

#

Same for the left?

grim igloo
#

Is to shift the string left or right
Say, string1[] = "Hello my friend \0";
Then the output of strShift(string1, 5); would be " Hello my friend\0"

woeful phoenix
#

ahh ok ok

grim igloo
#

Though I must also put '\0' at the end if shifting right

woeful phoenix
#

You don't need to add the null character there.

grim igloo
#

I just tend to be more safe

woeful phoenix
#

When you use string literal syntax, it automatically adds it!

grim igloo
#

And when you do not?

#

or what do you mean

woeful phoenix
#

I think you need to be mindful of the spaces in your shifting.

#

they are apart of the the string.

#

It would actually be easier if you instead of shifting in place, realloced space for your string, fill it with spaces, and then place the string inside the buffer accordingly.

grim igloo
#

Nonono

#

I want to like actually shift the string

#

Like bitwise shifting - even if loosing some parts

woeful phoenix
#

ahh hmm

grim igloo
#

So strShift("Lol who even made this code", -10); may output smth like "en made this code".

#

I am using it to make a function that prints a string, and skips to the next line if it's too long and so on.

#

Something like this:

void OutStrLines(const char* String, const short limit) {
    short length = strLen(String);
    char tempStr[length];
    strInit(tempStr, length);
    if (strCopy(String, tempStr) == 5) { 
        printf("AAAA"); 
        return;
    }
    short lines = 0;

    printf("%s\n", tempStr);
    fflush(stdout);
    printf("%d %d\n", strLen(tempStr), strLen(String));
    fflush(stdout);

    while(length > 0) {
        if ( length <= limit ) {
            write(1, tempStr, length);
            break;
        }
        write(1, tempStr, limit);
        strShift(tempStr, -limit);
        length = strLen(tempStr);
        lines++;
        putchar('\n');
    }
}```
#

Expected output with "Hello everybody, my name is Markiplier, and welcome back to Five Nights at Freddy's\n" and limit 15 is

Hello everybody
, my name is Ma
rkiplier, and w
elcome back to 
Five Nights at
Freddy's

But I just get

Hello everybody
�I��

Which is probably a problem with strShift

woeful phoenix
#
void strShift(char* string, int N) {
    int length = strlen(string);

    if(N == 0) return;
    else if(N < 0) {
        for(int i = 0; i < length; i++) {
          string[i] = string[-N + i];
        }
    } else {
        for(int i = length - N - 1; i >= 0; i--) {
          string[i + N] = string[i];
        }
        for(int i = 0; i < N; i++) {
          string[i] = ' ';
        }
    }
}
#

maybe something like this.

grim igloo
#

I'll try, though I didn't intend for filling the space with.. spaces. More like just changing
"Hello world\n\0" after strShift(str, 5); into "\0\0\0\0\0Hello \0"

#

Yo it works

#

What did you do

woeful phoenix
#

I thought about it!

grim igloo
#

No really

#

I see no change, other than that you used strlen, instead of defined by me strLen (which works faster, I checked), else(if)-s instead of several if-s and few things in loops

#

Is it because of int N instead of const short N?

woeful phoenix
#
void strShift(char* string, int N) {
    int length = strlen(string);
  
    if(N == 0) return;
    else if(N < 0) {
        for(int i = 0; i < length - (-N); i++) {
          string[i] = string[-N + i];
        }
        for(int i = 0; i < (-N); i++) {
         string[length - (-N) + i] = ' ';
       }
    } else {
        for(int i = length - N - 1; i >= 0; i--) {
          string[i + N] = string[i];
        }
        for(int i = 0; i < N; i++) {
          string[i] = ' ';
        }
    }
}
#

A minor fix.

woeful phoenix
#

In your loop condition.

grim igloo
#

I had

        for(int i = 0; i < Len -N; i++) {
            string[i] = string[i+N];
        }
        return;
    }```
woeful phoenix
#

So you had your loop going out of bounds.

grim igloo
#

Wait

#

Oh it was basically i < Len + N oops

woeful phoenix
#

Also, i + N is also out of bounds.

grim igloo
#

Yeah I forgot negative numbers are not positive numbers

#

Welp, thank you, kind stranger

woeful phoenix
#

yeye

#

np

grim igloo
#

Now I am finally finished with basic output for today, can finally commit

woeful phoenix
#

What is your strLen implementation?

#

It really shouldn't be faster.

grim igloo
#

It looks bit cursed but this is for some reason faster than a normal loop

short strLen(const char* string) {
    for(int i = 0; i < WORD_ULIMIT;)
    {
        switch(string[i]) {
            case'\0': return i;
            default:     break;
        }
        switch(string[i+1]) {
            case'\0': return i+1;
            default: i+=2; continue;
        }
    }
    return 0;
}```
woeful phoenix
#

I think usually the implementation is optimized assembly.

#

That uses vectorized operations.

#

But that's neat.

grim igloo
#

It isn't noticable normally, but when doing it 100000 times, it took 0.08 seconds, and strlen took 0.12 seconds

#

And it was consisted all over my 15 tests, so you can't say my laptop just lagged

#

Anyways

woeful phoenix
#

you gotta do a bit more than 15!

grim igloo
#

15 is enough for me

#

Anyways, how to close this post thingy

woeful phoenix
#

we just let it die silently

#

maybe

#

!done