tags:

views:

207

answers:

5

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!

+8  A: 

You're looking for the strrchr function.

char *str = strrchr("abcda", 'a');
int index = strlen("abcda")-strlen(str);
ACoolie
+2  A: 

Finding first 'a':

strchr("abcda", 'a'); // == "abcda"'s pointer

Finding last 'a':

strrchr("abcda", 'a'); // == "abcda"'s pointer + 4
viraptor
+5  A: 

Instead of returning i as soon as you've found an occurrence, store i in another variable.

ie. replace return i; with lastOccurence = i;

lastOccurrence should be initialized to -1 at the start of the function then returned at the end.

Also, your inner loop needs to check for s[j] != '\0' or you are going to go past the end of your input string while you search.

Trevor
A: 

As other answers have indicated there are a couple of approaches to this (homework assignment).

First would be to actually look for a standard library function that already does the job for you. On a UNIX or Linux system you might find that using man -k string and scanning the list of string functions for words like: find, search, and locate. From that you might have found the strchr() and strrchr() functions. The latter of these does exactly what you're asking about.

Assuming that your instructor wouldn't accept such a trivial solution you then have two obvious approaches ... scan "forward" through the string, always remembering the most recent place you "saw" the target character, or scan "backwards" through the string and breaking out of your loop the first time you see the target character.

The meat of the assignment is in how you scan (iterate) strings as they are implemented in C. The sad fact is that there's no advantage to scanning the string backwards (unless you've been given either a length or pointer to the end of the string as well as one to its head). If you call strlen() or the safer strnlen() (which is a common extension, but not technically a standard C library function) ... then you've implicitly incurred the cost/overhead of a full string scan).

So the only reasonably efficient approach to this assignment is to scan forward through the string, keeping track of the most recently spotted matching character, until you find the end of the string (an ASCII NUL character). As Trevor pointed out it's conventional to use a "sentinel" value of -1 to represent the case where no instance of the target was found.

Jim Dennis
A: 

It looks like the answers to date have ignored the fact that you want to search for a character or characters. So here's a simple solution using the interface you defined, allowing a search for a string. Sorry I haven't tried to compile and test this.

int strindex( const char s[], const char t[] )
{             
    int ret = -1; /* return -1 if target not found */
    int len1 = strlen(s);
    int len2 = strlen(t);
    if( len1 >= len2 )
    {
        int i;
        for( i=len1-len2; i>=0; i-- )
        {
            if( 0 == memcmp(s+i,t,len2) )
            {
                ret = i; /* found target t at idx i */
                break;        
            }
        }
    }
    return ret;    
}
Bill Forster