Hey all, I'm trying to find the numeric position of a character or number of characters in a string. I'm able to figure out how to see where character "a" is in the string "abcd" but if I put "abcda" it only prints out 0, meaning that it counts only the first instance. I want to find the last or rightmost occurence of this string. Here's what I have so far:
#include <stdio.h>
main(){
char s[20];
char t[20];
int pp;
printf("Enter a FULL string: \n");
scanf("%s", s);
printf("Enter what you want to find: \n");
scanf("%s", t);
pp = strindex(s, t);
printf("%d", pp);
}
/* string index */
int strindex(char s[], char t[]){
int i, j, k, c;
for (i = 0; s[i] != '\0'; i++){
for (j=i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++);
if (k > 0 && t[k] == '\0')
return i;
}
return -1;
}
It's probably pretty simple, but I've been working on it and racking my brains and nothing is coming out of it. Thanks a lot!