#unique function implementation

48 messages · Page 1 of 1 (latest)

abstract gorge
#

Hi! We got a strange task at university and I literally can't even get started. Maybe I'm just misunderstanding it or something, but anyways. We need to implement a function: char* unique(char* first, char* last) that takes in 2 pointers, a start and end pointer to a string and removes all characters that are the same as the last one. The catch that it cannot move the string, it returns a new last pointer, so first stays the same.

lost boughBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

abstract gorge
#

It would be easy to implement if I was allowed to move the string, but like this I don't know how to do it.

lean stump
#

Say the string is

abccd
^   ^

where the ^ denotes the start and end pointer, then the result should be

abcdx
^  ^

The value of x doesn't matter, could be c or d or something else.

#

Not sure what you want to move here.

abstract gorge
#

i mean that i cant just allocate a new char[] and edit that

lean stump
#

Sure you can. You just have to copy the end result back into the original string once you're done.

#

Though it's unnecessary.

abstract gorge
#

hmm is it thinkies

lean stump
abstract gorge
#

i mean i could shift the whole array to the left when i find a repeating character

lean stump
#

||Or you could swap with the last character and decrement the last pointer.||

lean stump
#

Not sure this actually works.

abstract gorge
#

well it doesnt ig ?

#

i mean

lean stump
#

Gotta mess with pointers a bit, maybe draw it on paper.

abstract gorge
#

abccdefg becomes abcgdef then no?

tender talon
lean stump
#

abcgdef is not acceptable?

tender talon
#

do you need to preserve order, or just remove the non-uniques?

lean stump
#

You could split your start pointer into a read pointer and a write pointer.

abstract gorge
#
char* unique(char* first, char* last) {
    for (char* iter = first+1; iter != last; iter++) {
        if (*iter == *(iter - 1)) {
            for (char* b_iter = iter; b_iter != last-1; b_iter++) {
                *b_iter = *(b_iter + 1);
            }
            last--;
        }
    }    
}
#

maybe something like this?

lean stump
#

You should really try not to potentially shift on every character.

#

aaaaaaaaaaaa shouldn't do however many as this is shifts.

#

If you do want to go the shift route consider making a helper function for that that you can test separately.

abstract gorge
#

i dont really know what you mean by that 😅

abstract gorge
#

but im pretty sure they want us to go the shifting way

lean stump
#

Write a function void shift_string(char *first, char *last).

#

That way your unique implementation becomes easier to understand and you can test the shifting separately.

#

This code is very prone to off-by-one errors and it's not at all easy to see where the error is.

abstract gorge
#

btw i tried and im not allowed to include cstring or string.h in this assignment

#

portal throws it back

#

it was accepted now tho

#

this was the check in task for tomorrow's class

#

we are supposed to be studying oop and we wrote 3 lines of c++ code in 3 weeks

lean stump
#

Sounds like a university or equivalent.

#

They don't really do code. Programming is a craft, not science, so they don't really care.

abstract gorge
#

i really fear that all my c++ knowledge will mean nothing and ill just do their bullshit for the semester

#

idk the last time i actually used raw pointers other than STL reimplementations for fun

lean stump
#

Your C++ knowledge from the class indeed means nothing at all. If you care to gain C++ knowledge you'll have to do so outside of the class. The class can still be useful to introduce and explain concepts.

abstract gorge
#

i meant the stuff i already know

#

i need the class so i can pass lol