Posted on 2008-06-03 17:06
路緣 閱讀(1234)
評論(0) 編輯 收藏 引用 所屬分類:
C/C++
今天看《C和指針》一書的第6章指針,碰到編程練習的第一題,做了做,感覺差距還很大,把做的代碼貼下來,以備往后溫習。
題目:
請編寫一個函數,它在一個字符串中進行搜索,查找所有在一個給定字符集合中出現的字符。這個函數的原型如下:char *find(char const *source, char const *chars);
它的基本想法是查找source字符串中匹配chars字符串中任何字符的第一個字符。函數然后返回一個指向source中第1個匹配所找到的位置的指針。如果source中的所有字符均不匹配chars中的任何字符,函數就返回一個NULL指針。如果任何一個參數為NULL,或任何一個參數所指向的字符串為空,函數也返回一個NULL指針。
舉個例子,假定source指向ABCDEF。如果chars指向XYZ、JUEY或QQQQ,函數就返回一個NULL指針。如果chars指向XRCQEF,函數就返回一個指向source中C字符的指針。參數所指向的字符串是絕不會被修改的。
a.你不應該使用任何用于操縱字符串的庫函數(如strcpy,strcmp,index等)。
b.函數中的任何地方都不應該使用下標引用
代碼:

/**//**//**//************************************************************************
* 文件名:CharFind.c
* 文件描述:得到字符串S1中第一個且是字符串S2中的位置指針的算法
* 創建人: Luyuan, 2008年6月3日
* 版本號:1.0
* 修改記錄:
*
************************************************************************/
#include <stdlib.h>
#include <stdio.h>

#define TRUE 1
#define FALSE 0


/**//********方法聲明********************************************************/
char *find_char(char const *source, char const *chars);
char *find_char2(char const *source, char const *chars);
char * my_strpbrk(const char *s1 ,const char *s2);

/**//***********************************************************************/

/**//*主函數*/
int main()


{
char *str1 = "xxammana_babi";
char *str2 = "babi";
char *p;

p = find_char2(str1, str2);

printf("The first char of found is %c\n", *p);

if( (p = my_strpbrk(str1,str2)) == NULL)
printf("No same character!\n");
else
printf("%c\n",*p);

str1 = "abc";
str2 = "def";

if( (p = my_strpbrk(str1,str2)) == NULL)
printf("No same character!\n");
else
printf("%c\n",*p);

system("pause");
return 0;
}


/**//***********************************************************************
* 函 數 名:find_char
* 參 數:
* char const *source [IN] : 進行查找的源字符串
char const *chars [IN]: 要查找的目標字符串
* 功能描述:
* 得到source中第一個且是chars中字符的位置指針。用下標
* 返 回 值:查找成功-找到的字符指針,失敗NULL
* 拋出異常:
* 作 者:Luyuan 2008-06-03
************************************************************************/
char *find_char(char const *source, char const *chars)


{
int iIndex=0, j=0;
const char *pS = NULL;
int iFound = FALSE;

if(source == NULL || chars == NULL)

{
return NULL;
}


for(; source[iIndex] != NULL; ++iIndex)/**//*遍歷源字符串字符*/

{
j = 0;

for(; chars[j] != NULL; ++j)/**//*遍歷目標字符串字符*/

{
if(source[iIndex] == chars[j])

{
iFound = TRUE;
break;
}
}


if(iFound)/**//*若iFound值為TRUE的話表示字符已經找到*/
break;
}

pS = &source[iIndex];
return pS;
}

/**//***********************************************************************
* 函 數 名:find_char
* 參 數:
* char const *source [IN] : 進行查找的源字符串
char const *chars [IN]: 要查找的目標字符串
* 功能描述:
* 得到source中第一個且是chars中字符的位置指針。沒有用下標
* 返 回 值:查找成功-找到的字符指針,失敗NULL
* 拋出異常:
* 作 者:Luyuan 2008-06-03
************************************************************************/
char *find_char2(char const *source, char const *chars)


{
int iIndex=0, j=0;
const char *pS = NULL;
int iFound = FALSE;

if(source == NULL || chars == NULL)

{
return NULL;
}


for(; *(source+iIndex) != NULL; ++iIndex)/**//*遍歷源字符串字符*/

{
j = 0;

for(; *(chars+j) != NULL; ++j)/**//*遍歷目標字符串字符*/

{
if(*(source +iIndex) == *(chars+j))

{
iFound = TRUE;
break;
}
}


if(iFound)/**//*若iFound值為TRUE的話表示字符已經找到*/
break;
}
if(!iFound)

{
return FALSE;
}
pS = (source+iIndex);
return pS;
}

/**//***********************************************************************
* 函 數 名:my_strpbrk
* 參 數:
* char const *s1 [IN] : 進行查找的源字符串
char const *s2 [IN]: 要查找的目標字符串
* 功能描述:
* 得到s1中第一個且是s2中字符的位置指針。
* 返 回 值:查找成功-找到的字符指針,失敗NULL
* 拋出異常:
* 作 者:來源C標準庫函數
************************************************************************/
char * my_strpbrk(const char *s1 ,const char *s2)


{
const char *c = s2;
if (!*s1)
return (char *) NULL;


while (*s1)/**//*遍歷源字符串字符*/

{

for (c = s2; *c; c++)/**//*遍歷目標字符串字符*/

{
if (*s1 == *c)
break;
}

if (*c)/**//*若當前字符不為字符串結尾字符“\n”之后,為NULL,則表示已經找到,條件為真*/
break;
s1++;
}

if (*c == '\0')
s1 = NULL;

return (char *) s1;
}

/**//********************************FILE END*********************************/

注意: 1.在上面的代碼中,注意有的地方聲明為const char *,而有些則聲明為char const *,因為無 const * 的定義,故這兩種定義沒有區別,都表示指向常量字符串的指針,即指針的指向的內容不可變,而指針本身可以改變。其次要注意與char * const 的區別,char * const 表示指向字符串的常量指針,即指針本身不可改變,而指針指向的內容可以改變。
2.我寫的代碼與庫函數有多處區別。首先我單獨定義了變量iFound來標識是否找到了匹配的字符,從而方便外層循環跳出。但由于做判斷并跳出的代碼再內層循環之后,而這時相應的指針已經指向目標字符串的末尾之后,是否找到匹配字符從指針本身即可做出判斷,額外設置變量標識,純屬多余。