diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-05 12:06:51 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2021-12-05 12:06:51 +0800 |
| commit | da633eb55be20b86a004a172ce5fe720ae5e8de9 (patch) | |
| tree | 00ebb6773db68545416d1735f43908fe5939ee94 /4/1.c | |
| parent | ebdf520db77ae0ae6594bb168697b90906138009 (diff) | |
| download | k&r-exercises-da633eb55be20b86a004a172ce5fe720ae5e8de9.tar.gz | |
4.1
Diffstat (limited to '4/1.c')
| -rw-r--r-- | 4/1.c | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -0,0 +1,35 @@ +#include <stdio.h> + +#define MAXLEN 1000 + +/* finds the rightmost occurrence of t in s */ +int strindex(char s[], char t); + +int main(int argc, char *argv[]) { + int i, c, t; + char s[MAXLEN]; + + printf("source: "); + for (i = 0; i < MAXLEN - 1 && (c = getchar()) != '\n' && c != EOF; i++) + s[i] = c; + s[i] = 0; + + printf("character: "); + t = getchar(); + printf("index: %d\n", strindex(s, t)); + + return 0; +} + +int strindex(char s[], char t) { + int i, index; + + index = -1; + + for (i = 0; s[i] != 0; i++) { + if (s[i] == t) + index = i; + } + + return index; +}
\ No newline at end of file |
