char *strrchr(const char *string, int c);
函式功能:找到特定字元最後一次出現在string中的位置,並回傳該位置的指標。
下列程式碼使用strrchr去找到字串中’o’最後一次出現的位址。
#include <stdio.h>
#include <string.h>
int main(void)
{
  char String1[12] = "hello world";
  char *ptr;
  int   find_ch = 'o';
  int Location = 0;
  ptr = strrchr( String1, find_ch );
  printf( "'%c' last show at '%s'\n",find_ch, ptr );
  Location = ptr - (int)(&String1);//透過兩個位址相減得到字元所在位置
  printf("%c is at String1[%d]\n",find_ch,Location);
}
執行效果如下
參考資料

沒有留言:
發佈留言