“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
#include <stdio.h> main() { char *x= “acbcd”; char y = ‘c’; int len = strlen(x); int i=0,j=0; for( i = len-1; i>= 0; i– ) { if ( x[i] == y ) { break; } } printf(”%s%c%s%d%s”,”the # of last occur of “,y,” is “,i+1,”n”); }
#include <stdio.h>
main() {
char *x= “acbcd”;
char y = ‘c’;
int len = strlen(x);
int i=0,j=0;
for( i = len-1; i>= 0; i– ) {
if ( x[i] == y ) {
break;
}
}
printf(”%s%c%s%d%s”,”the # of last occur of “,y,” is “,i+1,”n”);
}
avdate your solution is to slow.
char *lastchar(char *pszStr,char chMatch)
{
char *pchrtn = NULL;
while( *pszStr++ != NULL )
if( *pszStr == chMatch ) pchrtn=pszStr;
return pchrtn;
}
too slow is avery dicey term. If its a very long string then it make a lot of good sense to start from the end.
in such a case your solution would be “too slow”
int lastinstance(char *s, char c)
{
int x=strlen(s);
for(; x>=0;x–)
{
if(s[x]==c)
return x+1;
}
return 0;
}
Doesn’t strlen() do a forward scan until it finds ‘