/*****************************************************************************
* @COPYRIGHT NOTICE
* @Copyright (c) 2013, ChenMH
* @All rights reserved
* @file : TestCharacter.h
* @version : ver 1.0
* @author : ChenMH
* @date : 2013/5/17 15:04
* @brief : C語言中,一種從字符串中獲取字符所在位置的方法。
是在偶然中從一處開源代碼裏看到,記錄之......
*****************************************************************************/
#include <string.h>
int main(int argc, char **argv)
{
//計算字符w在字符串string中的位置
char* string = "Hello world!";
char c = 'w';
char* ptr = strchr(string, c);
int pos = ptr-string;
if (ptr)
printf("The character [%c] was found at pos: [%d]\n", c, pos);
else
printf("The character was not found\n");
return 0;
}
/**************************************************************************
* @Copyright (c) 2013, ChenMH, All rights reserved.
* @file : main.cpp
* @version : ver 1.0
* @author : ChenMH
* @date : 2013/05/17 16:45
* @brief : 使用strrchr截取字符串
**************************************************************************/
#include <cstdio>
#include <cstring>
//(C語言)從當前文件的路徑中截取文件名,及獲取文件名首字母在字符串中的位置
int main(int argc, char **argv)
{
const char* pName = strrchr(*argv,'/');
if (!pName) pName = strrchr(*argv,'\\');
if (pName) pName++;
int pos = pName - *argv;
printf("file path: [%s]\n", *argv);
printf("file name: [%s]; pos: [%d]\n", pName, pos);
return 0;
}