#A way to shift Strings to the left or right (C)
65 messages · Page 1 of 1 (latest)
Would you mind posting your code and not just a picture?
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;
}
}```
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?
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"
ahh ok ok
Though I must also put '\0' at the end if shifting right
You don't need to add the null character there.
I just tend to be more safe
When you use string literal syntax, it automatically adds it!
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.
Nonono
I want to like actually shift the string
Like bitwise shifting - even if loosing some parts
ahh hmm
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
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.
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
I thought about it!
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?
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.
The first half you may have not thought about N being negative.
In your loop condition.
I had
for(int i = 0; i < Len -N; i++) {
string[i] = string[i+N];
}
return;
}```
So you had your loop going out of bounds.
Also, i + N is also out of bounds.
Yeah I forgot negative numbers are not positive numbers
Welp, thank you, kind stranger
Now I am finally finished with basic output for today, can finally commit
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;
}```
I think usually the implementation is optimized assembly.
That uses vectorized operations.
But that's neat.
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
you gotta do a bit more than 15!