#Finding the last occurrence of a character in a string

7 messages · Page 1 of 1 (latest)

scarlet sableBOT
#

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.

solemn plinth
#

seems to be alright

civic nacelle
#

start from the end and do reverse iteration

eager prism
#

was going to say, the printing seems fine, but the logic could probably use some work

#

;compile -fanalyzer```c
#include <stdio.h>
#include <stddef.h>
struct qstr {
size_t length;
size_t capacity;
char* data;
};
void qstr_rmlast (struct qstr* s, char c) {
int index = -1;
for (int i = 0; i < s->length; i++) {
if (s->data[i] == c) index = i;
}
printf("%d\n", index);
}
int main () {
struct qstr s = {5, 5, "hello"};
qstr_rmlast(&s, 'l');
}

zenith moonBOT
#
Program Output
3
eager prism
#

works fine either way