1. 编写一个函数,它在一个字符串中进行搜索,查找一个给定字符集合中出现的所有字符。这个函数的原型如下:
char *find_char( char const *source, char const *chars );
它的基本想法是查找source字符串中匹配chars字符串中任何字符的第1个字符,然后返回一个指向source中第1个匹配所找到的位置的指针。如果source中的所有字符均不匹配chars中的任何字符,就返回一个NULL指针。如果任何一个参数为NULL,或任何一个参数所指向的字符串为空,函数也返回一个NULL指针。
举个例子,假定source指向ABCDEF,如果chars指向XYZ、JURY或QQQQ,函数就返回一个NULL指针;如果chars指向XRCQEF,函数就返回一个指向source中C字符的指针。参数所指向的字符串是绝不会被修改的。
碰巧,C函数库中存在一个名叫strpbrk的函数,它的功能几乎和这个要编写的函数一模一样。但这个程序的目的是让你自己练习操纵指针,所以:
a.不应该使用任何用于操纵字符串的库函数(如strcpy、strcmp、index等);
b.函数中的任何地方都不应该使用下标引用。
解析:
#include <stdio.h>
#include <stdlib.h>
char *find_char( char const *source, char const *chars );
int main( void ){
const char *pc = "ABCDEF";
char *pfc;
const char *pchars = "XRZQEF";
pfc = find_char( pc, pchars );
if( pfc == NULL ){
printf( "%s's any character is not in %s.\n", pchars, pc );
} else{
printf( "%s's first character in %s is %c.\n", pchars, pc, *pfc );
}
return EXIT_SUCCESS;
}
char *find_char( char const *source, char const *chars ){
/* use to specify special condition */
if( source == NULL || chars == NULL ||
*source == '\0' || *chars == '\0' ){
return NULL;
}
while( *source != '\0' ){
char const *pc = chars;
while( *pc != '\0' ){
if( *source != *pc ){
++pc;
} else{
return (char *)source;
}
}
++source;
}
return NULL;
}
输出:
2.编写一个函数,删除一个字符串的一部分。函数的原型如下:
int del_substr( char *str, char const *substr )
函数首先应该判断substr是否出现在str中。如果它并未出现,函数就返回0;如果出现,函数应该把str中位于该子串后面的所有字符复制到该子串的位置,从而删除这个子串,然后函数返回1。如果substr多次出现在str中,函数只删除第1次出现的子串。函数的第2个参数绝不会被修改。
举个例子,假定str指向ABCDEFG,如果substr指向FGH、CDF或XABC,函数应该返回0,str未做任何修改;如果substr指向CDE,函数就把str修改为指向ABFG,方法是把F、G和结尾的NUL字节复制到C的位置,然后函数返回1。不论出现什么情况,函数的第2个参数都不应该被修改。
和上题的程序一样:
a.不应该使用任何用于操纵字符串的库函数(如strcpy、strcmp、index等);
b.函数中的任何地方都不应该使用下标引用。
一个值得注意的地方是,空字符串是每个字符串的一个子串,在空字符串中删除一个空字符串不会产生变化。
解析:
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
int del_substr( char *str, char const *substr );
/*
** this function isn't the answer the question,
** it will delete characters in str that appear in substr.
*/
int del_substr_2( char *str, char const *substr );
int main( void ){
char str[] = "ABCDEFG";
char const *substr = "EFH";
int result;
printf( "original str is %s\n", str );
result = del_substr( str, substr );
if( result == 0 ){
printf( "the characters in %s aren't included in %s perfectly.\n", substr, str );
} else{
printf( "delete substr %s successed, present str is %s.\n", substr, str );
}
return EXIT_SUCCESS;
}
int del_substr( char *str, char const *substr ){
char *str_temp;
/* use to indicate whether find the same substr */
int flag = 0;
/* substr is a null string */
if( *substr == '\0' ){
return TRUE;
}
for( ; *str != '\0'; ++str ){
str_temp = str;
char const *sub_temp = substr;
while( *str_temp == *sub_temp && *sub_temp != '\0'){
str_temp += 1;
sub_temp += 1;
}
if( *sub_temp == '\0' ){
flag = 1;
break;
}
}
/* reach the end of substr */
if( flag ){
/* don't reach the end of str */
for( ; *str_temp != '\0'; ++str_temp ){
*str++ = *str_temp;
}
*str = '\0';
return TRUE;
}
return FALSE;
}
int del_substr_2( char *str, char const *substr ){
char *str_temp;
char *str_temp2;
int flag = FALSE;
if( *substr == '\0' ){
return TRUE;
}
while( *str != '\0' ){
char const *pc = substr;
while( *pc != '\0' ){