20 lines
416 B
C
20 lines
416 B
C
/*
|
|
* Exercise 4-1. Write the function strindex(s,t) which returns the position of
|
|
* the rightmost occurrence of t in s, or -1 if there is none
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
int strindex(char s[], char t) {
|
|
for (int i = 0; i < strlen(s); i++) {
|
|
if (s[i] == t) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int main() {
|
|
int index = strindex("Hello world", 'w');
|
|
printf("Index: %d\n", index);
|
|
}
|