memset:
- /*
- * memset – Fill a region of memory with the given value
- * @s: Pointer to the start of the area.
- * @c: The byte to fill the area with
- * @count: The size of the area.
- */
- void *memset(void *s, int c, size_t count)
- {
- char *xs = s;
- while (count––)
- *xs++ = c;
- return s;
- }
memcpy:
- /*
- * memcpy – Copy one area of memory to another
- * @dest: Where to copy to
- * @src: Where to copy from
- * @count: The size of the area.
- */
- void *memcpy(void *dest, const void *src, size_t count)
- {
- char *tmp = dest;
- const char *s = src;
- while (count–)
- *tmp++ = *s++;
- return dest;
- }
memmove:
- /*
- * memmove – Copy one area of memory to another
- * @dest: Where to copy to
- * @src: Where to copy from
- * @count: The size of the area.
- * Unlike memcpy(), memmove() copes with overlapping areas.
- */
- void *memmove(void *dest, const void *src, size_t count)
- {
- char *tmp;
- const char *s;
- if (dest <= src) {
- tmp = dest;
- s = src;
- while (count––)
- *tmp++ = *s++;
- } else {
- tmp = dest;
- tmp += count;
- s = src;
- s += count;
- while (count––)
- *––tmp = *––s;
- }
- return dest;
- }
memcmp:
- /*
- * memcmp – Compare two areas of memory
- * @cs: One area of memory
- * @ct: Another area of memory
- * @count: The size of the area.
- */
- int memcmp(const void *cs, const void *ct, size_t count)
- {
- const unsigned char *su1, *su2;
- int res = 0;
- for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count––)
- if ((res = *su1 – *su2) != 0)
- break;
- return res;
- }
strcpy:
- /*
- * strcpy – Copy a %NUL terminated string
- * @dest: Where to copy the string to
- * @src: Where to copy the string from
- */
- char *strcpy(char *dest, const char *src)
- {
- char *tmp = dest;
- while ((*dest++ = *src++) != ‘\0‘);
- return tmp;
- }
strncpy:
- /*
- * strncpy – Copy a length–limited, %NUL–terminated string
- * @dest: Where to copy the string to
- * @src: Where to copy the string from
- * @count: The maximum number of bytes to copy
- *
- * The result is not %NUL–terminated if the source exceeds
- * @count bytes.
- *
- * In the case where the length of @src is less than that of
- * count, the remainder of @dest will be padded with %NUL.
- */
- char *strncpy(char *dest, const char *src, size_t count)
- {
- char *tmp = dest;
- while (count) {
- if ((*tmp = *src) != 0)
- src++;
- tmp++;
- count––;
- }
- return dest;
- }
strcat:
- /*
- * strcat – Append one %NUL–terminated string to another
- * @dest: The string to be appended to
- * @src: The string to append to it
- */
- char *strcat(char *dest, const char *src)
- {
- char *tmp = dest;
- while (*dest)
- dest++;
- while ((*dest++ = *src++) != ‘\0‘);
- return tmp;
- }
strncat:
- /*
- * strncat – Append a length–limited, %NUL–terminated string to another
- * @dest: The string to be appended to
- * @src: The string to append to it
- * @count: The maximum numbers of bytes to copy
- *
- * Note that in contrast to strncpy(), strncat() ensures the result is
- * terminated.
- */
- char *strncat(char *dest, const char *src, size_t count)
- {
- char *tmp = dest;
- if (count) {
- while (*dest)
- dest++;
- while ((*dest++ = *src++) != 0) {
- if (––count == 0) {
- *dest = ‘\0‘;
- break;
- }
- }
- }
- return tmp;
- }
strcmp:
- /*
- * strcmp – Compare two strings
- * @cs: One string
- * @ct: Another string
- */
- int strcmp(const char *cs, const char *ct)
- {
- unsigned char c1, c2;
- while (1) {
- c1 = *cs++;
- c2 = *ct++;
- if (c1 != c2)
- return c1 < c2 ? –1 : 1;
- if (!c1)
- break;
- }
- return 0;
- }
strncmp:
- /*
- * strncmp – Compare two length–limited strings
- * @cs: One string
- * @ct: Another string
- * @count: The maximum number of bytes to compare
- */
- int strncmp(const char *cs, const char *ct, size_t count)
- {
- unsigned char c1, c2;
- while (count) {
- c1 = *cs++;
- c2 = *ct++;
- if (c1 != c2)
- return c1 < c2 ? –1 : 1;
- if (!c1)
- break;
- count––;
- }
- return 0;
- }
strchr:
- /*
- * strchr – Find the first occurrence of a character in a string
- * @s: The string to be searched
- * @c: The character to search for
- */
- char *strchr(const char *s, int c)
- {
- for (; *s != (char)c; ++s)
- if (*s == ‘\0‘)
- return NULL;
- return (char *)s;
- }
strlen:
- /*
- * strlen – Find the length of a string
- * @s: The string to be sized
- */
- size_t strlen(const char *s)
- {
- const char *sc;
- for (sc = s; *sc != ‘\0‘; ++sc);
- return sc – s;
- }
strnlen:
- /*
- * strnlen – Find the length of a length–limited string
- * @s: The string to be sized
- * @count: The maximum number of bytes to search
- */
- size_t strnlen(const char *s, size_t count)
- {
- const char *sc;
- for (sc = s; count–– && *sc != ‘\0‘; ++sc);
- return sc – s;
- }
strsep:
- /*
- * strsep – Split a string into tokens
- * @s: The string to be searched
- * @ct: The characters to search for
- *
- * strsep() updates @s to point after the token, ready for the next call.
- */
- char *strsep(char **s, const char *ct)
- {
- char *sbegin = *s;
- char *end;
- if (sbegin == NULL)
- return NULL;
- end = strpbrk(sbegin, ct);
- if (end)
- *end++ = ‘\0‘;
- *s = end;
- return sbegin;
- }
strstr:
- /*
- * strstr – Find the first substring in a %NUL terminated string
- * @s1: The string to be searched
- * @s2: The string to search for
- */
- char *strstr(const char *s1, const char *s2)
- {
- int l1, l2;
- l2 = strlen(s2);
- if (!l2)
- return (char *)s1;
- l1 = strlen(s1);
- while (l1 >= l2) {
- l1––;
- if (!memcmp(s1, s2, l2))
- return (char *)s1;
- s1++;
- }
- return NULL;
- }
常见字符串面试题:
1)写出在母串中查找子串出现次数的代码.
int count1(char* str,char* s)
{
char* s1;
char* s2;
int count = 0;
while(*str!='/0')
{
s1 = str;
s2 = s;
while(*s2 == *s1&&(*s2!='/0')&&(*s1!='0'))
{
s2++;
s1++;
}
if(*s2 == '/0')
count++;
str++;
}
return count;
}
2)查找第一个匹配子串位置,如果返回的是s1长度len1表示没有找到
size_t find(char* s1,char* s2)
{
size_t i=0;
size_t len1 = strlen(s1)
size_t len2 = strlen(s2);
if(len1-len2<0) return len1;
for(;i<len1-len2;i++)
{
size_t m = i;
for(size_t j=0;j<len2;j++)
{
if(s1[m]!=s2[j])
break;
m++;
}
if(j==len)
break;
}
return i<len1-len2?i:len1;
}
3)实现strcpy函数
char *strcpy(char *destination, const char *source)
{
assert(destination!=NULL&&source!=NULL);
char* target = destinaton;
while(*destinaton++=*source++);
return target ;
}
出现次数相当频繁
4)实现strcmp函数
int strcmp11(char* l,char* r)
{
assert(l!=0&&r!=0);
while(*l == *r &&*l != '/0') l++,r++;
if(*l > *r)
return 1;
else if(*l == *r)
return 0;
return -1;
}
5) 实现字符串翻转
void reserve(char* str)
{
assert(str != NULL);
char * p1 = str;
char * p2 = str-1;
while(*++p2); //一般要求不能使用strlen
p2 -= 1;
while(p1<p2)
{
char c = *p1;
*p1++ = *p2;
*p2-- = c;
}
}
6)、用指针的方法,将字符串“ABCD1234efgh”前后对调显示
//不要用strlen求字符串长度,这样就没分了
代码如下:
char str123[] = "ABCD1234efgh";
char * p1 = str123;
char * p2 = str123-1;
while(*++p2);
p2 -= 1;
while(p1<p2)
{
char c = *p1;
*p1++ = *p2;
*p2-- = c;
}
7) 给定字符串A和B,输出A和B中的最大公共子串。比如A=”aocdfe” B=”pmcdfa” 则输出”cdf”
#i nclude<stdio.h>
#i nclude<stdlib.h>
#i nclude<string.h>
char *commanstring(char shortstring[], char longstring[])
{
int i, j;
char *substring=malloc(256);
if(strstr(longstring, shortstring)!=NULL) //如果……,那么返回shortstring
return shortstring;
for(i=strlen(shortstring)-1;i>0; i--) //否则,开始循环计算
{
for(j=0; j<=strlen(shortstring)-i; j++)
{
memcpy(substring, &shortstring[j], i);
substring[i]='/0';
if(strstr(longstring, substring)!=NULL)
return substring;
}
}
return NULL;
}
main()
{
char *str1=malloc(256);
char *str2=malloc(256);
char *comman=NULL;
gets(str1);
gets(str2);
if(strlen(str1)>strlen(str2)) //将短的字符串放前面
comman=commanstring(str2, str1);
else
comman=commanstring(str1, str2);
printf("the longest comman string is: %s/n", comman);
}
8) 判断一个字符串是不是回文
int IsReverseStr(char *str)
{
int i,j;
int found=1;
if(str==NULL)
return -1;
char* p = str-1;
while(*++p!= '/0');
--p;
while(*str==*p&&str<p) str++,p--;
if(str < p)
found = 0;
return found;
}
9)写函数完成内存的拷贝
void* memcpy( void *dst, const void *src, unsigned int len )
{
register char *d;
register char *s;
if (len == 0)
return dst;
if ( dst > src ) //考虑覆盖情况
{
d = (char *)dst + len - 1;
s = (char *)src + len - 1;
while ( len >= 4 ) //循环展开,提高执行效率
{
*d-- = *s--;
*d-- = *s--;
*d-- = *s--;
*d-- = *s--;
len -= 4;
}
while ( len-- )
{
*d-- = *s--;
}
}
else if ( dst < src )
{
d = (char *)dst;
s = (char *)src;
while ( len >= 4 )
{
*d++ = *s++;
*d++ = *s++;
*d++ = *s++;
*d++ = *s++;
len -= 4;
}
while ( len-- )
{
*d++ = *s++;
}
}
return dst;
}
出现次数相当频繁
10)写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:”abcd12345ed125ss123456789″的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789
int continumax(char *outputstr, char *inputstr)
{
char *in = inputstr, *out = outputstr, *temp, *final;
int count = 0, maxlen = 0;
while( *in != '/0' )
{
if( *in > 47 && *in < 58 )
{
for(temp = in; *in > 47 && *in < 58 ; in++ )
count++;
}
else
in++;
if( maxlen < count )
{
maxlen = count;
count = 0;
final = temp;
}
}
for(int i = 0; i < maxlen; i++)
{
*out = *final;
out++;
final++;
}
*out = '/0';
return maxlen;
}
11) 编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。
char * search(char *cpSource, char ch)
{
char *cpTemp=NULL, *cpDest=NULL;
int iTemp, iCount=0;
while(*cpSource)
{
if(*cpSource == ch)
{
iTemp = 0;
cpTemp = cpSource;
while(*cpSource == ch)
++iTemp, ++cpSource;
if(iTemp > iCount)
iCount = iTemp, cpDest = cpTemp;
if(!*cpSource)
break;
}
++cpSource;
}
return cpDest;
}
排序算法:
冒泡排序: 出现次数相当频繁
void buble(int *a,int n)
{
for(int i=0;i<n;i++)
{
for(int j=1;j<n-i;j++)
{
if(a[j]<a[j-1])
{
int temp=a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
}
插入排序:
void insertsort(int* a,int n)
{
int key;
for(int j=1;j<n;j++)
{
key = a[j];
for(int i=j-1;i>=0&&a[i]>key;i--)
{
a[i+1] = a[i];
}
a[i+1] = key;
}
}
将一个数字字符串转换为数字.”1234″ –>1234
int atoii(char* s)
{
assert(s!=NULL);
int num = 0;
int temp;
while(*s>'0' && *s<'9')
{
num *= 10;
num += *s-'0';
s++;
}
return num;
}