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.
#unique function implementation
48 messages · Page 1 of 1 (latest)
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.
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.
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.
i mean that i cant just allocate a new char[] and edit that
Sure you can. You just have to copy the end result back into the original string once you're done.
Though it's unnecessary.
hmm is it 
Unless your task explicitly says you're not allowed to.
i mean i could shift the whole array to the left when i find a repeating character
||Or you could swap with the last character and decrement the last pointer.||
wait what 
Not sure this actually works.
Gotta mess with pointers a bit, maybe draw it on paper.
abccdefg becomes abcgdef then no?
that's what I had in mind, but haven't validated it either
abcgdef is not acceptable?
do you need to preserve order, or just remove the non-uniques?
You could split your start pointer into a read pointer and a write pointer.
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?
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.
i dont really know what you mean by that 😅
this also doesnt work i forgot an iter-- after shifting
but im pretty sure they want us to go the shifting way
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.
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

Sounds like a university or equivalent.
They don't really do code. Programming is a craft, not science, so they don't really care.
yeah i mentioned it in my question :'D
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
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.